开发者

jQuery label 'for' attribute selector

I am using Remy Sharp's labelover plugin for jQuery and I would like to exclud开发者_Go百科e a label with the attribute for and value nature.

Here's an example of the code working:

$(document).ready(function() {
    $('form.default label').labelOver('over');
});

and what I'm trying to do:

$(document).ready(function() {
    $('form.default label').not($('label').attr('for','nature')).labelOver('over');
});

Can anyone see where I'm going wrong? Feels like I'm pretty close to what I need to do.


attr is not a selector, it's a function that gets the attribute value with attribute name as the 1st argument, or sets it with a new value if one is passed as a 2ng argument.

Also, you excluded labels after selecting them with your not call, because the selector label matched all labels, and attr as I said did not filter that.

To select based on attribute, use this:

$(document).ready(function() {
    $("form.default label[for!='nature']").labelOver('over');
});

As you may have guessed, the [attribute='value'] is the selector for an attribute "equal" to some value, and [attribute!='value'] is the "not equal" version of it.

For reference see:
http://api.jquery.com/attribute-not-equal-selector/

For reference on all selectors:
http://api.jquery.com/category/selectors/

This is also referenced at my JavaScript & Web Dev Newsletter site.


.attr('for', 'nature') is setting the value for the for attribute to nature

To filter by attributes, use [attribute="value"]:

$('form.default label').not('[for="nature"]').labelOver('over')


working code: http://jsfiddle.net/3nQbr/1/

$('label').not('[for="nature"]').labelOver('over');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜