jQuery 如何在多个属性值上选择元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8045071/
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-08-27 09:55:34  来源:igfitidea点击:

How do I select elements on multiple attribute values

jquery

提问by user380719

With jQuery, it is easy to select elements with a given attribute value.

使用 jQuery,可以轻松选择具有给定属性值的元素。

For example:

例如:

var elements = $('div[attr1="value1"]');

But how do I select on multiple attributes (e.g., attr1 = value1andattr2 = value2)?

但是我如何选择多个属性(例如,attr1 = value1attr2 = value2)?

回答by Dmitry B.

Since jquery uses CSS selectors, as defined by the CSS specification a selector with multiple conditions will look like:

由于 jquery 使用 CSS 选择器,根据 CSS 规范的定义,具有多个条件的选择器将如下所示:

$('div[attr1="value1"][attr2="value2"]')

see the CSS spec for further reference: http://www.w3.org/TR/CSS2/selector.html#matching-attrs

请参阅 CSS 规范以获取进一步参考:http: //www.w3.org/TR/CSS2/selector.html#matching-attrs

回答by SunnyRed

You could for example chain and filter like so

例如,您可以像这样链接和过滤

var elements = $('div[attr1="value1"]').filter('div[attr2="value2"]');

回答by Balasubramani M

Find this solution quite simple.

找到这个解决方案非常简单。

$('[attr1="home"][attr2="settings"]')

回答by VISHNU Radhakrishnan

To select multiple attributes, see the code below.

要选择多个属性,请参阅下面的代码。

This code will find all inputs that have an idattribute and whose name attribute ends with 'man' and sets the value.

此代码将查找具有id属性且名称属性以“man”结尾的所有输入并设置值。

$( "input[id][name$='man']" ).val( "this input has id and name ends with 'man'" );