[英]jQuery - select multiple elements with the same attribute value
I am trying to find a jQuery selector that gets every element with a specific attribute value - not necessarily the same attribute , just the same attribute value. 我试图找到一个jQuery选择器,该选择器获取具有特定属性值的每个元素-不一定是相同的属性 ,而只是相同的属性值。 For example, what jQuery selector would pick up each of the following?
例如,哪个jQuery选择器将选择以下每个选项?
<h1 name="test"><h1>
<a id="test"></a>
<p data-attribute="test"></p>
All I can think to try is $("*").attr( "*", "test" )
but that does not work. 我只能尝试尝试
$("*").attr( "*", "test" )
但这不起作用。
This is a very odd question. 这是一个非常奇怪的问题。 It can be accomplished by looping through the attributes of each dom attribute, but I would recommend instead restructuring your HTML so you can use
[data-test=true]
, a data-test attribute with the value of true. 可以通过遍历每个dom属性的属性来完成此操作,但是我建议改为重组HTML,以便您可以使用
[data-test=true]
,即值为true的数据测试属性。
$("*").filter(function() { flag = false; $.each(this.attributes, function(i, attrib){ if (attrib.value == "test") { flag = true; } }); return flag; }).css("background","red");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1 name="test">a<h1> <a id="test">b</a> <p data-attribute="test">c</p> <p>don't highlight</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.