我有一个带有标题和无序列表的文档。
如何使用JQuery选择给定的标题(通过其惟一的类名)以及该标题和下一个标题之间的所有内容?
更新:
你的建议很棒,但不是我想要的。例如,在下面的代码中,我只想访问id为"heading2“的"h1”和之前的所有内容,但不包括id为"heading3“的"h1”。
上面提供的jQuery示例将访问第一个不是"h“标记的"h”标记之后的所有内容。
..。或者,如果我错了,请纠正我:)
<h1 id="heading1">...</h1>
<ul>...</ul>
<p>...</p>
<ul>...</ul>
<p>...</p>
<h1 id="heading2" >...</h1>
<ul>...</ul>
<p>...</p>
<ul>...</ul>
<p>...</p>
<h1 id="heading3" >...</h1>
<ul>...</ul>
<p>...</p>
<ul>...</ul>
<p>...</p>
发布于 2009-01-26 21:50:27
有两种方法对解决这个问题特别有用:.nextUntil
和.andSelf
。第一个方法将获取选择器后面的所有兄弟项,而后者也将包含与选择器匹配的所有内容,从而为您提供一个包含它们的jQuery对象:
$("#heading2")
.nextUntil("#heading3").andSelf()
.css("background", "red");
这将导致以下结果:
发布于 2010-04-06 01:30:19
$('#secondSelector').prevAll('#firstSelector ~ *')
发布于 2009-01-27 00:03:43
是啊,你说得对。这只会避开所需的选择器。也许它需要更详细:
$(firstSelector).nextAll().not(secondSelector).not($(secondSelector).nextAll()).text()
https://stackoverflow.com/questions/481076
复制相似问题