gpt4 book ai didi

javascript - 从第 n 个标签及以上获取 html

转载 作者:行者123 更新时间:2023-12-03 02:33:19 25 4
gpt4 key购买 nike

我有一张 table ...

        <table class="col-xs-12">
<thead class="testhead col-xs-12">
<tr class="col-xs-12" id="row1" style="color: white;">
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody class="testbody col-xs-12">
<tr class="testing col-xs-12" id="row0" style="color: white;">
<td class="pTest">0 col 0</td>
<td class="pTest">0 col 1</td>
<td class="pTest">0 col 2</td>
<td class="pTest">0 col 3</td>
<td class="pTest">0 col 4</td>
</tr>

<tr class="testing col-xs-12" id="row1" style="color: white;">
<td class="pTest">1 col 0</td>
<td class="pTest">1 col 1</td>
<td class="pTest">1 col 2</td>
<td class="pTest">1 col 3</td>
<td class="pTest">1 col 4</td>
</tr>

<tr class="testing col-xs-12" id="row2" style="color: white;">
<td class="pTest">2 col 0</td>
<td class="pTest">2 col 1</td>
<td class="pTest">2 col 2</td>
<td class="pTest">2 col 3</td>
<td class="pTest">2 col 4</td>
</tr>

<tr class="testing col-xs-12" id="row3" style="color: white;">
<td class="pTest">3 col 0</td>
<td class="pTest">3 col 1</td>
<td class="pTest">3 col 2</td>
<td class="pTest">3 col 3</td>
<td class="pTest">3 col 4</td>
</tr>

<tr class="testing col-xs-12" id="row4" style="color: white;">
<td class="pTest">4 col 0</td>
<td class="pTest">4 col 1</td>
<td class="pTest">4 col 2</td>
<td class="pTest">4 col 3</td>
<td class="pTest">4 col 4</td>
</tr>
</tbody>
</table>

我想将其分成两个变量:

  • Var 1 是 TR 的第一个 x(自定义数量)
  • Var 2 是其余的 TR

我希望它包含 <tr>标签也是如此。

我尝试过使用 jQuery nextAllnextUntiltr:gt(1)但它们的效果并不好。

例如,即使我将 gt 设置为大于 1,它也只给了我 1 TR 并且还排除了 <tr>标签!

var hello = $(".testbody tr:gt(1)").html();
console.log(hello);

最佳答案

jQuery 的 .html() 仅获取第一个匹配元素的 html。您需要循环遍历您的元素并自行组合 html。

var hello = $('.testbody tr:gt(1)');
var html = '';
hello.each(function() {
html = html + $(this).html()
});

例如。

编辑:.html()也只会获取内部html,而不是元素本身的标签/html。您可能需要找出另一种方法

关于javascript - 从第 n 个标签及以上获取 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48642390/

25 4 0