使用jquery在某些属性中查找HTML中的元素

时间:2022-05-20 21:26:17

If I have the following HTML

如果我有以下HTML

<Something attribute="" id="top">
    <Something attribute="child" id="child"></Something>
</Something>

I am trying to get a jquery object representing the second Something element. I am trying the following query (as suggested in this question Find all elements with a certain attribute value in jquery) in my browser console

我试图获得一个表示第二个Something元素的jquery对象。我在浏览器控制台中尝试以下查询(在此问题中建议在jquery中查找具有特定属性值的所有元素)

$("Something[attribute='child']")

But I seem to be getting the same empty array each time, however the following works

但我似乎每次都得到相同的空数组,但以下工作

$("Something[attribute='']")

This returns me an array of one element representing the first Something element

这将返回一个表示第一个Something元素的元素数组

1 个解决方案

#1


0  

This will return a DOM array of divs which have id (attribute) "stack":

这将返回一个具有id(属性)“stack”的div的DOM数组:

var stack = $('div[id=stack]');

var result = " ";

$(stack).each(function(index, element){
    result = element.id + result;
});

document.body.innerHTML = result
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<div id="stack"></div>
<div id="stack"></div>
<div id="notstack"></div>
<div id="notstack"></div>

#1


0  

This will return a DOM array of divs which have id (attribute) "stack":

这将返回一个具有id(属性)“stack”的div的DOM数组:

var stack = $('div[id=stack]');

var result = " ";

$(stack).each(function(index, element){
    result = element.id + result;
});

document.body.innerHTML = result
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<div id="stack"></div>
<div id="stack"></div>
<div id="notstack"></div>
<div id="notstack"></div>