使用Javascript从不同表中的表行获取元素

Getting elements from table row within different tables with Javascript

本文关键字:获取 元素 使用 Javascript      更新时间:2023-09-26

我需要用javascript(jquery)从tr返回一些元素

问题1。我怎么能到达td哪里是评论"我需要得到这个项目(基于question1)"?所以我需要得到最后一个项目与类"b"和它之后的第一个td ?td的数量不是静态的。类b不能在同一个表中

问题2。另外,如果我想选择类"a"并给出下一个5(例如)td的一些类(例如"b")我怎么能做到呢?函数nextAll可以提供帮助,但它只能从单行获取元素。

示例如下:

<table>
<tr>
  <td></td>
  <td></td>
  <td class="a"></td>
  <td></td> <!-- this needs to get class b -->
  <td></td> <!-- this needs to get class b -->
  <td></td> <!-- this needs to get class b -->
</tr>
</table>
<table>
<tr>
  <td></td> <!-- this needs to get class b -->
  <td></td> <!-- this needs to get class b -->
  <td></td> <!-- i need to get to this item (based on question1) -->
  <td></td>
  <td></td> 
  <td></td>
</tr>
</table>

谢谢

你可以这样做

$(document).ready(function() {
    $("td.b:last").html(); // to get content of last td with class b 
    $("td.b:last").next("td").html(); // to get first td after b class 
    // for a 
    $("td.a:first").slice(5).addClass("b");
 });
 </script>
参考<<p>看到strong>:去年, :第一个片()

新编辑

  $("table.someclass").each(function(){
        $(this).find("td.a:first").nextAll("td").addClass("b");
    });