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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:51:59  来源:igfitidea点击:

jQuery Attribute Selectors OR Operation

javascriptjqueryjquery-selectorscss-selectors

提问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 land aattributed elements? I searched for a selector with OR, something like this:

如何仅更改la属性元素的颜色?我用 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');
});

Fiddle: http://jsfiddle.net/maniator/2Xuat/

小提琴:http: //jsfiddle.net/maniator/2Xuat/