开发者

jQuery function equal to href=#

开发者 https://www.devze.com 2023-01-24 05:01 出处:网络
Is there a jquery function that can make you go to #name, like you can if you make a link 开发者_Go百科to href=\"#name\", so I could on document ready directly go to #nameYou can just change the locat

Is there a jquery function that can make you go to #name, like you can if you make a link 开发者_Go百科to href="#name", so I could on document ready directly go to #name


You can just change the location.hash, like this:

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

Though if you just hyperlink to the page, this is built-in browser behavior, for example going to http://site.com/page.html#name would have the same scroll-on-load effect.


There is JavaScript:

window.location.hash = "#name"

https://developer.mozilla.org/en/DOM/window.location


While a bit overkill you can try the scrollto plugin:

Scrollto


<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> 

Or

<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> 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号