开发者

For each table row / cell with specific class function

开发者 https://www.devze.com 2023-03-15 04:27 出处:网络
I have a table. The table has some set of rows, each set containing 3 specific rows, and repeated downwards.

I have a table. The table has some set of rows, each set containing 3 specific rows, and repeated downwards.

I want to have a variable i which should be incremented for each row (<tr>) having class="nm" or for each cell (<td>) having class="zxcv". The incremented result value i may be alerted finally.

How can I do this using JavaScript with / without jQuery?

You can see my JS Fidd开发者_如何学运维le here.


If you just want to get a count of <tr class="nm"> and <td class="zxcv">, using jQuery, do this:

alert($('tr.nm, td.zxcv').length);

EDIT:

To loop through each element, do this:

$('tr.nm, td.zxcv').each(function(index, elem) { /* do something */ });


I find your wording a bit unclear, but if you're trying to do something to each row with the class "nm" you could use the .each() function:

$('.nm').each(function(i,row) {
    //$(row) is now your .nm row
    alert(i);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消