繁体   English   中英

根据特定属性的值获取元素的类或ID名称

[英]Getting class or ID name of an element based on the value of a particular attribute

有没有办法获取具有属性特定值的所有元素的类或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>
<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。

您可以使用.map()属性等于选择器

var array = $('rect[selected="1"]').map(function(){
    return this.className;
}).get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM