我试图始终使用单个 Jsoup select() 语句找到后面的第一个,最好不循环。 <ul><h2>
例如,它<ul>可以是<h2>以下 HTML 片段中所示的兄弟:
<!-- lots of things can come before -->
<h2 class="C" id="S3">
<button class="B">Click Me</button>
<span id="targetSpan">Target Span</span>
</h2>
<ul>
<li> List item 1</li>
<li> List item 2</li>
</ul>
<!-- lots of things can come after -->
或者它可以是 的兄弟姐妹的后代(不一定是直系孩子!)<h2>。兄弟元素可能是也可能不是之后的第一个兄弟元素<h2>,但<ul>始终是<ul>之后的第一个元素<h2>。例如:
<!-- lots of things can come before -->
<h2 class="C" id="S3">
<button class="B">Click Me</button>
<span id="targetSpan">Target Span</span>
</h2>
<div>
<ul>
<li> List item 1</li>
<li> List item 2</li>
</ul>
</div>
<!-- lots of things can come after -->
我可以<h2>很容易地找到:
Element h2 = select("h2 > span#targetSpan").first().parent();
但是一旦我有了h2,我如何找到<ul>它之后的第一个?(可能是兄弟姐妹或后代,我不控制该 HTML 代码)