2010-06-11 61 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(); 
});