2010-04-29 57 views

回答

3

在你的document.ready就可以調用錨的單擊事件,像這樣:

$(function() { 
    if(document.location.hash != '') 
    $(document.location.hash).click(); 
}); 

這將使例如www.yoursite.com/yourPage.htm#art2該鏈路上進行點擊使其打開。只要確保上面的代碼是點擊事件處理程序後,所以它實際上在點擊:)

如果您使用的命名錨登陸#art,類似的做法東西:

$(function() { 
    if(document.location.hash != '') 
    $('a[name="' + document.location.hash.replace('#','') + '"]').click(); 
}); 
+0

+ 1。但'.hash'包含「#」? – Joel 2010-04-29 19:58:25

+0

@Joel - 是的,我相信在所有瀏覽器中都是一致的。 – 2010-04-29 20:00:06

+0

我無法讓它工作。在這裏做了一個測試頁: http://www.libraries.uc.edu/tests/table_rows.html 這樣你的例子就是: http://www.libraries.uc.edu/tests/table_rows .html#art2 – LisaH 2010-04-29 20:08:40