2010-09-22 129 views
1

我想知道如何通过点击链接来获得最后一个孩子的提醒。选择最后一个孩子

<h3 id="comment:5" class="myclass"> 

这是多远我得到:

$('#lastcomment').click(function() { 
    alert('Handler for .click() called.'); 
}); 

我的目的是通过点击链接,跳转到最后一个注释。我猜,第一步可能会发出警报。这样我可以看到评论的最后一个孩子。

任何帮助,非常感谢。

+0

你能给的要提醒什么的例子吗? – 2010-09-22 11:18:11

+0

TI想要提醒最新评论。在这种情况下评论:9。 – Faili 2010-09-22 11:19:39

回答

3

使用:last选择或.last()方法:

$("#lastcomment").click(function() { 
    var lastComment = $("h3[id^=comment]:last"); 

    alert(lastComment.attr("id")); 
} 
+0

几乎是我正在寻找的答案。 – Faili 2010-09-22 11:26:57

+0

你不知道一个函数的名称,而不是提醒跳转到评论? – Faili 2010-09-22 11:35:12

+0

@Faili:你应该可以使用'window.location.hash = lastComment.attr(“id”);' – 2010-09-22 11:38:56

0

你可以使用jQuery的:last选择器。但我需要看到更多的HTML来使一些工作...

0

因此,你有一个链接,你想要去一个页面上的最后一个评论?为何不给评论评论-12(例如)的ID,那么就改变你的锚relfect此:

<a class="myclass" href="#comment-12" id="last-comment"> </a> 

如果你想做到这一点在JavaScript中,是这样的:

$('#lastcomment').click(function() { 
    var numComments = $(".comments").length; // Assuming your comments have a class 
    window.location = window.location.href + "#" + $(".comments").eq(numComments).attr("id"); 
});