简体   繁体   中英

For each table row / cell with specific class function

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 Fiddle 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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM