简体   繁体   English

JQuery在表行中向tds添加编号

[英]JQuery add numbering to tds within a table row

I have come up with this function to add numbered classes to the elements within tables rows in my table: 我已经想出了这个函数来为我表中的表行中的元素添加编号类:

$('tr td:contains(".")').each(function(i){
     $(this).addClass("reportcellno-" + i);
});

Basically here, I am searching for any table cell with a decimal point and I want to interate through them within each row and add the class reportcellno-1 , reportcellno-2 基本上在这里,我正在搜索任何带小数点的表格单元格,我想在每一行中进行交互并添加类reportcellno-1reportcellno-2

This works pretty well and I have played around with it all day. 这很好用,我整天都在玩它。 The only issue is that the numbering goes on and on rather than limiting it by row. 唯一的问题是编号一直在继续,而不是按行限制。

My output HTML code from the above is: 我从上面输出的HTML代码是:

<tr>
<td class="reportcellno-1">10.1</td>
<td class="reportcellno-2">5.7</td>
</tr>

<tr>
<td class="reportcellno-3">10.6</td>
<td class="reportcellno-4">10.9</td>
</tr>

Whereas I actually am trying to get this: 虽然我实际上想要得到这个:

<tr>
<td class="reportcellno-1">10.1</td>
<td class="reportcellno-2">5.7</td>
</tr>

<tr>
<td class="reportcellno-1">10.6</td>
<td class="reportcellno-2">10.9</td>
</tr>

So essentially for each table row, I want to start the numbering over. 所以基本上对于每个表行,我想开始编号。 I am not even sure if this is possible. 我甚至不确定这是否可行。

Go by tr instead: 转而去tr

$('tr').each(function() {
   $(this).children('td:contains(".")').each(function(i) {
      $(this).addClass("reportcellno-" + i);
   });
});

EDIT: less-loop way, but probably less readable: 编辑:循环方式较少,但可能性较差:

$('tr td:contains(".")').each(function(){
   $(this).addClass("reportcellno-" + (+$(this).index() + 1));
});​

Here, we are using the fact that a td is a child of a tr , and index() returns the position of an element relative to its siblings. 这里,我们使用的事实是tdtr的子tr ,而index()返回元素相对于其兄弟节点的位置。

From the docs : 来自文档

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements. 如果没有将参数传递给.index()方法,则返回值是一个整数,指示jQuery对象中第一个元素相对于其兄弟元素的位置。

A plain js solution, numbered in rows: 一个普通的js解决方案,按行编号:

var rows = document.getElementsByTagName('tr');

for (var j=0, jLen=rows.length; j<jLen; j++) {
  var cell, cells = rows[j].getElementsByTagName('td');

  for (var i=0, iLen=cells.length, c=0, txt; i<iLen; i++) {
    cell = cells[i];
    txt = cell.innerText || cell.textContent;

    if ( txt.indexOf('.') > -1) {
      cell.className = cell.className + ' reportcellno-' + c++;
    }
  } 
}

Numbered continuously through the table: 在表格中不断编号:

var cell, cells = document.getElementsByTagName('td');

for (var i=0, iLen=cells.length, c=0, txt; i<iLen; i++) {
  cell = cells[i];
  txt = cell.innerText || cell.textContent;

  if (txt.indexOf('.') > -1) {
    cell.className = cell.className + ' reportcellno-' + c++;
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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