开发者

Jump to a element automatically on page load

i have elements like this on page

<a href="#hi">go to hi开发者_如何学Go</a>
.
.
<a name="hi">hi text here</a>

but i would like users go to "hi text here" at first on page loading. how to do this?


I'd suggest that you first test for another hash before moving your users' browser to focus another element:

if (!document.location.hash){
    document.location.hash = 'hi';
}

JS Fiddle demo.

Incidentally, you can use the hash (the part after the # in the URL) to jump to any element that has an id, you don't need to use the named-anchors (<a name="hi">...</a>).


Either, you can use the URL with the anchor (mysite.com/#hi) or you could use javascript:

document.getElementById('hi').scrollIntoView(true);

Please note that you should use ID, not name.


i think it would be usefull to work with anchors:

<a name="hi">here goes the text information what you like</a>

Create a link to the "hi Section" inside the same document:

<a href="#hi">Go to hi</a>

Or, create a link to the "hi Section" from another page:

<a href="http://www.stackoverflow.com/html_links.htm#hi">
Visit the hi Section</a> 


You can use jQuery along with its scrollTo plugin, and then write something like this:

$.scrollTo("a[name = hi]");

It should work just fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜