我有这个 HTML 结构:
<h2>title 1</h2>
<p>bla</p>
<p>bla</p>
<h2>title 2</h2>
<p>bla</p>
<p>blabla</p>
<p>bla</p>
<h2>title 3</h2>
<p>bla</p>
H2之后的P元素的数量是不同的。
H2
和 P
元素是我正在寻找的一组元素。是否有任何解决方案来获取这组元素或任何其他方式来查找 H2(或文档末尾)之间的 P 所有元素?
我检查了.nextAll()
函数,但它找到了所有接下来的P元素,直到下一个H2。
最佳答案
要获取 h2
元素之间或末尾的每组 p
元素,请使用以下命令:
var $h2 = $("h2"); // get the h2 elements
$h2.each(function() { // for each one of them ...
var $p = $(this).nextUntil("h2", "p"); // get the p elements untill the next h2 element or the end
// do stuff with this group of p elements
}
关于javascript - 如何通过 jQuery 获取 P 元素直到下一个 H2 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114825/