选择最后一个孩子

我想知道如何通过点击链接获得最后一个孩子的警报。

这是我得到了多远:

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

我的目标是通过单击链接跳转到最后一条评论。 我想第一步就是警报就可以了。 这样我才能看到评论的最后一个孩子。

任何帮助深表感谢。

使用:last selector或.last()方法:

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

你可以使用jQuery :last选择器。 但我需要看到更多的HTML才能使某些东西发挥作用……

所以你有一个链接,你想去页面上的最后一个评论? 为什么不给评论id为comment-12(例如),然后只需更改你的锚点即可:

   

如果您想在javascript中执行此操作,请执行以下操作:

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