使用jQuery搜索并替换REL属性

时间:2011-03-28 15:50:38

标签: jquery

如果rel属性目前等于rel,我想搜索链接并替换其attachement属性。

我到目前为止的代码是:

$("href").each(function(index) {
    $(this).attr('rel', 'group');
});

4 个答案:

答案 0 :(得分:2)

如果你试图针对某些事情测试rel值(让我们说"附件"),你所要做的就是这样:

$('a[rel="attachment"]').attr('rel','group');

这表示对于<a>的所有rel="attachment"元素,请将rel设置为&#34; group&#34;。

另一方面,如果您要更改的内容是具有href属性的所有元素,那么您将使用

之类的内容
$('[href][rel="attachment"]').attr('rel','group');

答案 1 :(得分:0)

$('a').each(function() {
  $(this).attr('rel', 'group')
});

不确定你试图测试'rel'的价值。

答案 2 :(得分:0)

没有href

这样的选择器

尝试:

$("a").each(function() {
    $(this).attr('rel', 'group');
});

答案 3 :(得分:0)

你的意思是等于"attachment"吗?

$("a").each(function(index) {
    // href is `this.href`
    if(this.rel == 'attachment') 
        $(this).attr('rel', 'group');  
});
相关问题