2

共通のカスタム属性 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>
4

2 に答える 2

2

試す:

document.querySelectorAll("[prId='" + 2 + "']").length

作業: http://jsfiddle.net/rLnTD/3/

于 2013-08-29T05:33:18.607 に答える
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.');
于 2013-08-29T05:38:40.683 に答える