Javascript jQuery 标签“for”属性选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6967453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery label 'for' attribute selector
提问by Jimmy Joyce
I am using Remy Sharp's labelover plugin for jQueryand I would like to exclude a label with the attribute for
and value nature
.
我正在为 jQuery使用 Remy Sharp 的labelover 插件,我想排除一个带有属性for
和值的标签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.
谁能看到我哪里出错了?感觉我已经非常接近我需要做的事情了。
回答by Meligy
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.
attr
不是选择器,它是一个函数,它以属性名称作为第一个参数获取属性值,或者如果一个新值作为 2ng 参数传递,则将其设置为新值。
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.
此外,您在通过not
呼叫选择label
标签后排除了标签,因为选择器匹配所有标签,并且attr
正如我所说的那样没有过滤。
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.
您可能已经猜到,the[attribute='value']
是属性“等于”某个值的选择器,[attribute!='value']
是它的“不等于”版本。
For reference see:
http://api.jquery.com/attribute-not-equal-selector/
参考:http:
//api.jquery.com/attribute-not-equal-selector/
For reference on all selectors:
http://api.jquery.com/category/selectors/
有关所有选择器的参考:http:
//api.jquery.com/category/selectors/
This is also referenced at my JavaScript & Web Dev Newsletter site.
回答by mak
.attr('for', 'nature')
is setting the value for the for
attribute to nature
.attr('for', 'nature')
正在将for
属性值设置为nature
To filter by attributes, use [attribute="value"]
:
要按属性过滤,请使用[attribute="value"]
:
$('form.default label').not('[for="nature"]').labelOver('over')
回答by Jacek Kaniuk
working code: http://jsfiddle.net/3nQbr/1/
工作代码:http: //jsfiddle.net/3nQbr/1/
$('label').not('[for="nature"]').labelOver('over');