Search and replace REL attribute with jQuery
I want to search links and replace their rel
attributes if the rel
attribute currently equal attachement
.
The code I have so far is:
开发者_运维问答$("href").each(function(index) {
$(this).attr('rel', 'group');
});
If you're trying to test the rel value against something (let's say "attachment"), all you have to do is something like this:
$('a[rel="attachment"]').attr('rel','group');
This says that for all <a>
elements with rel="attachment"
, set rel
to "group".
On the other hand, if what you mean to change is all elements with an href
attribute, you'll use something like
$('[href][rel="attachment"]').attr('rel','group');
$('a').each(function() {
$(this).attr('rel', 'group')
});
Not sure what you're trying to test the value of 'rel' against.
no such selector as href
try:
$("a").each(function() {
$(this).attr('rel', 'group');
});
Do you mean equals "attachment"
?
$("a").each(function(index) {
// href is `this.href`
if(this.rel == 'attachment')
$(this).attr('rel', 'group');
});
精彩评论