javascript 对于具有特定类功能的每个表格行/单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6443894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:41:36  来源:igfitidea点击:

For each table row / cell with specific class function

javascriptjqueryjquery-selectors

提问by blasteralfred Ψ

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

我有一张桌子。该表有一组行,每组包含 3 个特定的行,并向下重复。

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

我想要一个变量i,它应该为每行 ( <tr>)class="nm"或每个单元格 ( <td>) 增加class="zxcv"。增加的结果值i可以最终被提醒。

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

我如何使用带有/不带有 jQ​​uery 的 JavaScript 来做到这一点?

You can see my JS Fiddle here.

你可以在这里看到我的 JS Fiddle

回答by FishBasketGordo

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

如果您只想使用 jQuery获取<tr class="nm">and的计数<td class="zxcv">,请执行以下操作:

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

EDIT:

编辑:

To loop through each element, do this:

要遍历每个元素,请执行以下操作:

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

回答by hughes

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()函数:

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