2010-11-08 76 views
2

是否有一個jQuery的功能,可以讓你去#name,就像你可以如果你做一個鏈接到href =「#name」,所以我可以在文檔準備直接去#名jQuery的功能等於href =#

回答

5

你可以只改變的location.hash,就像這樣:

$(function() { 
    window.location.hash = '#name'; //works with or without the # included 
}); 

但如果你只是超鏈接到網頁,這是內置的瀏覽器的行爲,例如將http://site.com/page.html#name將具有相同滾動加載效果。

1

雖然有點大材小用,你可以嘗試scrollto插件:

Scrollto

0
<html> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
</head> 
<body> 
<a id="goto" href="#this">go to this</a> 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> 
<div id="this">this</div> 
</body> 
</html> 
<script type="text/javascript"> 
    $(function(){ 

     $("#goto")[0].click(); 

    }); 
</script> 

或者

<html> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
</head> 
<body> 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> 
<div id="this">this</div> 
</body> 
</html> 
<script type="text/javascript"> 
    $(function(){ 

     $('<a href="#this">go to this</a>')[0].click(); 

    }); 
</script>