簡體   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