Javascript 基于特定属性的值获取元素的类或ID名称
是否有任何方法可以获取具有特定属性值的所有元素的类或ID名称?例如,如果我有以下内容:Javascript 基于特定属性的值获取元素的类或ID名称,javascript,jquery,html,Javascript,Jquery,Html,是否有任何方法可以获取具有特定属性值的所有元素的类或ID名称?例如,如果我有以下内容: <rect class="rect0" x="45" y="0px" width="40px" height="40px" fill="#ff0000" selected="0"></rect> <rect class="rect1" x="90" y="0px" width="40px" height="40px" fill="#ff0000" selected="0">
<rect class="rect0" x="45" y="0px" width="40px" height="40px" fill="#ff0000" selected="0"></rect>
<rect class="rect1" x="90" y="0px" width="40px" height="40px" fill="#ff0000" selected="0"></rect>
<rect class="rect2" x="135" y="0px" width="40px" height="40px" fill="#ff0000" selected="0"></rect>
<rect class="rect3" x="180" y="0px" width="40px" height="40px" fill="#0400fb" selected="1"></rect>
<rect class="rect4" x="225" y="0px" width="40px" height="40px" fill="#ff0000" selected="0"></rect>
<rect class="rect5" x="270" y="0px" width="40px" height="40px" fill="#0400fb" selected="1"></rect>
<rect class="rect6" x="315" y="0px" width="40px" height="40px" fill="#fb0004" selected="0"></rect>
我想获取所有矩形的类名,其中所选属性的值为1,在本例中,将返回rect3和rect5 您可以使用&
你试过写一些代码吗?
var array = $('rect[selected="1"]').map(function(){
return this.className;
}).get();