Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于特定属性的值获取元素的类或ID名称_Javascript_Jquery_Html - Fatal编程技术网

Javascript 基于特定属性的值获取元素的类或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">

是否有任何方法可以获取具有特定属性值的所有元素的类或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

您可以使用&


你试过写一些代码吗?
var array = $('rect[selected="1"]').map(function(){
    return this.className;
}).get();