搜索和使用jQuery

问题描述:

我要搜索链接并换购rel属性如果rel属性当前等于attachement更换REL属性。搜索和使用jQuery

我到目前为止的代码是:

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

如果你想测试对某事的相对值(比方说,“附件”),所有你需要做的就是这样的事情:

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

这是说,对于所有<a>rel="attachment"元件,设置rel到“基团”。

在另一方面,如果你的意思改变的是与href属性的所有元素,你会使用类似

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

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

不知道你想测试“相对”反对的价值是什么。

没有这样的选择是href

尝试:

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

'href' * *是一个有效的[元素选择](HTTP: //api.jquery.com/element-selector/),它将选择任何名称为* href *的元素。但HTML中没有* href *元素。 – Gumbo 2011-03-28 16:09:21

+0

@Gumbo。那就是我的意思:-p – Neal 2011-03-28 16:09:41

你的意思等于"attachment"

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