计算表中具有特定单元格内容的行

时间:2016-11-28 23:45:30

标签: javascript html

我想用javascript计算具有特定单元格内容的行 这是表格的HTML代码:

<table class="table table-bordered" id="UsersDataTable">
    <thead>
      <tr>
        <th>H0</th>
        <th>H1</th>
        <th>H2</th>
        <th>H3</th>
        <th>H4</th>
        <th>H5</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>C0</td>
        <td>C1</td>
        <td>C2</td>
        <td>C3</td>
        <td>C4</td>
        <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
      </tr>
    </tbody>
</table>

如何使用'alt="Green"'计算所有单元格? 到目前为止,我有这段代码:

function CountRows(TableID, alt) {
    var refTab = document.getElementById(TableID)
    var counter = 0
    for ( var i = 0; row = refTab.rows[i]; i++ ) {
        alert(refTab.rows[i].cells[5].innerHTML);
        if (refTab.rows[i].cells[5].innerHTML === alt)
        {
            counter ++
        }
    }

    return counter
}

谢谢你的帮助 帕特里克

3 个答案:

答案 0 :(得分:2)

您可以使用x <- c(TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE) sum(lapply(split(x, cumsum(!x)), sum)>=2) #[1] 3 获取您想要的内容,如下所示。然后获取返回数组的beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", id1, id2, id3));。这是你的计数。

&#13;
&#13;
document.querySelectorAll("#UsersDataTable [alt='Green']")
&#13;
length
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果你可以使用jQuery,可以这样做:

var allElementsWithAltAttribute = $("[alt=green]");

或者您可以在纯JS中使用文档:

var allElementsWithAltAttribute = document.querySelectorAll('[alt=green]');

得到计数:

var count = allElementsWithAltAttribute.length;

答案 2 :(得分:0)

您可以使用.querySelector('[alt="'+alt+'"]')检查传递的alt中是否有任何元素,因此您的代码可能如下:

if (refTab.rows[i].cells[5].querySelector('[alt="'+alt+'"]')!=null)
{
  counter++;
}

希望这有帮助。

&#13;
&#13;
function CountRows(TableID, alt) {
  var refTab = document.getElementById(TableID)
  var counter = 0
  for ( var i = 0; row = refTab.rows[i]; i++ ) {
    if (refTab.rows[i].cells[5].querySelector('[alt="'+alt+'"]')!=null)
    {
      counter++;
    }
  }

  return counter;
}

alert( CountRows('UsersDataTable','Green') );
&#13;
<table class="table table-bordered" id="UsersDataTable">
  <thead>
    <tr>
      <th>H0</th>
      <th>H1</th>
      <th>H2</th>
      <th>H3</th>
      <th>H4</th>
      <th>H5</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
    </tr>

    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td>C5</td>
    </tr>

    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
    </tr>

    <tr>
      <td>C0</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td><center><img src=".\pictures\green.jpg" class="img-circle" alt="Green" width="20" height="20"></center></td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;