我在遍历jquery中的书签时遇到了问题。具体地说,是以下HTML:
<a id="comment-1"></a>
<div class="comment">
<h2 class="title"><a href="#comment-1">1st Post</a></h2>
<div class="content">
<p>this is 1st reply to the original post</p>
</div>
<div class="test">1st post second line</div>
</div>如果页面在URL (site.com/test.html#comment-1)中带有书签标签,我会尝试遍历到class = "title“的位置。以下是我用于测试的代码:
if(window.location.hash) {
alert ($(window.location.hash).nextAll().html());
}它执行得很好,并返回适当的html (<h2 class="title"><a href="#co...)
问题是如果我给它添加一个选择器($(window.location.hash).next('.title').html() ),我会得到一个空的结果。为何会这样呢?nextAll不是正确的遍历函数吗?(我也尝试过next+find,但没有用)
谢谢!
发布于 2010-04-19 09:33:35
$('#comment-1')选择器选择<a>元素。next方法查看该元素的下一个同级节点。没有包含"title“类的节点,因此得到的结果为空。在您的示例中,<a>的唯一兄弟节点是带有注释“class=”的div。要查找<h2 class="title">元素,可以使用以下命令:
$(window.location.hash).next().children('.title')发布于 2010-09-01 06:14:01
有一个jquery插件可以做到这一点:http://github.com/shanbady/Jquery-ajaxBookmarkable
https://stackoverflow.com/questions/2664743
复制相似问题