javascript jQuery 属性选择器 OR 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6497883/
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 Attribute Selectors OR Operation
提问by trrrrrrm
Here is my html
:
这是我的html
:
<div id="l">l</div>
<div id="a">a</div>
<div id="i">i</div>
How to change the color of only the l
and a
attributed elements? I searched for a selector with OR, something like this:
如何仅更改l
和a
属性元素的颜色?我用 OR 搜索了一个选择器,如下所示:
$(function(){
$('div[id=l,a]').css('color','red');
});
It's not working, is there something like that in jQuery?
它不起作用,jQuery 中有类似的东西吗?
EDITThank you guys it's working now:
编辑谢谢你们现在正在工作:
$('[id=l], [id=a]').css('color', 'red');
But what if I want to search for those IDs inside a <div>
like $('[id=l], [id=a]','div')
. This doesn't work, how should I do it?
但是如果我想在<div>
like 中搜索这些 ID 呢$('[id=l], [id=a]','div')
?这不起作用,我该怎么办?
回答by Naftali aka Neal
$(function(){
$('#l, #a').css('color','red');
});