我需要抓取一个如下所示的网站:
<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>
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/