2010-09-22 88 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"); 
});