我正在拥有一个包含多个行的表,这些行具有公共自定义属性prId。我怎么能发现没有。具有相同自定义属性的行。我不能使用jquery
<tr id="mainRow2" prId = "2"></tr>
<tr id="subRow2_1" prId = "2"></tr>
<tr id="subRow2_2" prId = "2"></tr>
<tr id="subRow2_3" prId = "2"></tr>
<tr id="mainRow5" prId = "5"></tr>
<tr id="subRow5_1" prId = "5"></tr>
<tr id="subRow5_2" prId = "5"></tr>
<tr id="subRow5_3" prId = "5"></tr>
<tr id="subRow5_4" prId = "5"></tr>
答案 0 :(得分:2)
答案 1 :(得分:1)
如上所示,如果不使用查询选择器(更好,但支持较少),则可以使用
var trs = document.getElementsByTagName('tr'), tr, i, count = 0;
for (i = 0; ( tr = trs[i] ); i += 1) {
// use your own attribute value here, of course
if (tr.getAttribute('prId') === '2') {
count += 1;
}
}
alert(count + ' prIds counted.');