javascript - jQuery - 获取某些标签之间的所有 html

标签 javascript jquery html

我需要抓取一个如下所示的网站:

<p></p>

<!-- I want to get HTML from here... -->

<h2> </h2>
<table> </table>

<h2> </h2>
<table> </table>

<h2> </h2>
<table> </table>

<!-- to here -->

<div> </div>

<h2> </h2>
<table> </table>

如上所述,我想获取 <p> 之间的所有 HTML和 <div> 。我不想要<h2><table>在 div 下方。

有什么好方法可以刮掉这些部分吗?如果可能的话,我想避免定位子索引。

谢谢

最佳答案

$('p').nextUntil('div').addClass('add')
.add{color:red}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>

<!-- I want to get HTML from here... -->

<h2> I want to get HTML from here...</h2>
<table><tr><td>I want to get HTML from here...</td></tr> </table>

<h2> I want to get HTML from here...</h2>
<table><tr><td>I want to get HTML from here...</td></tr> </table>

<h2> I want to get HTML from here...</h2>
<table><tr><td>I want to get HTML from here...</td></tr> </table>

<!-- to here -->

<div> </div>

<h2> </h2>
<table> </table>

使用.nextUntil()

Get all following siblings of each element up to but not including the element matched by the selector, DOM node, or jQuery object passed.

关于javascript - jQuery - 获取某些标签之间的所有 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38968250/

相关文章:

javascript - 将 jQuery 与切换事件结合使用

php - jquery - 使用 Lightbox 插件和来自 mysql 数据库的 blob

javascript - 使用 JQueryRotate 逆时针旋转图像

javascript - 取消服务器已收到的 AJAX 调用

javascript - Electron 将图像设置为壁纸(OS X 和 Windows)

javascript - 需要使用javascript在文本框中显示加法结果

html - 使用 CSS 选择器选择最后一个元素

html - 定位的绝对 div 未出现在定义的位置

javascript - 如何防止用户为每种情况选择单选按钮?

javascript - jQuery 和 "this"管理?如何避免变量冲突?