2010-06-11 77 views
5

腳本塊添加id,得出:jQuery的跳躍ID(不包括動畫)

<div id="some">Text</div> 

之後頁面應該跳轉爲id,沒有動畫,就像我們的目標#some上當前頁面鏈接。

如何做到這一點?

謝謝。

回答

10

你僅僅意味着連接 「老派」 像#some

例如

<a href="#some"></a> 

完成在JS與

location.href = "#some"; 
3

像這樣:

location.href = "#some"; 
19

試試這個:

document.getElementById('some').scrollIntoView(true); 
+4

+1因爲我從來沒有聽說過這個!驚訝它跨瀏覽器,儘管不屬於規範的一部分。 http://www.quirksmode.org/dom/w3c_cssom.html#t23 – Andrew 2010-06-11 14:51:35

2
$('a[href^="#"]').click(function() { 
$('html,body').animate({ scrollTop: $(this.hash).offset().top}); 
return false; 
e.preventDefault(); 
});