jquery v1.3.2 按属性查找元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/696968/
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 v1.3.2 find element by attribute
提问by Gennady Shumakher
I need to find and iterate through all child elements that have specific attribute. The following code worked fine in jquery 1.2.6, but throws exception in 1.3.2
我需要查找并遍历所有具有特定属性的子元素。以下代码在 jquery 1.2.6 中运行良好,但在 1.3.2 中引发异常
$(parentElement).find('*[@someAttributeName]').each(function(index){
doSomething(this);
});
What is the correct way to achieve that?
实现这一目标的正确方法是什么?
回答by tvanfosson
Just get rid of the @, I believe.
只要摆脱@,我相信。
$(parentElement).find('[someAttributeName]').each(function(index){
doSomething(this);
});
From the jQuery selectordocs:
来自 jQuery选择器文档:
Note: In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the '@' symbol from your selectors in order to make them work again.
注意:在 jQuery 1.3 中 [@attr] 样式选择器被删除(它们之前在 jQuery 1.2 中被弃用)。只需从选择器中删除“@”符号即可使它们再次工作。
回答by Konstantin Tarkus
Note the "@" before the attribute name was deprecated as of version 1.2.
请注意从 1.2 版起不推荐使用属性名称之前的“@”。
$(parentElement).find('*[someAttributeName]').each(function(index){
doSomething(this);
});
Just remove it and you are good to go.
只需将其删除即可。
回答by Seb
[@attribute]
notation is deprecated in jQuery 1.3. Remove the @
sign and you're good to go.
[@attribute]
表示法在 jQuery 1.3 中已弃用。取下@
标志,您就可以开始了。
回答by Yuseferi
ithink this is the best way to find and can change something of it
我认为这是找到并可以改变它的最佳方式
$('.youritem').each(function(){
if($(this).attr('title') == 'add image')
$(this).attr('id','imageuploader');
});
回答by rofrol
Doesn't work for me in IE when I want to find inputs with required=""
.
当我想用required=""
.
Works when I change to required="required"
. Maybe other combinations also work https://stackoverflow.com/a/3012975/588759
当我更改为required="required"
. 也许其他组合也有效https://stackoverflow.com/a/3012975/588759