繁体   English   中英

在页面加载时自动跳转到元素

[英]Jump to a element automatically on page load

我在页面上有这样的元素

<a href="#hi">go to hi</a>
.
.
<a name="hi">hi text here</a>

但我希望用户首先在页面加载时转到“hi text here”。 这个怎么做?

在移动用户的浏览器以关注另一个元素之前,我建议您首先测试另一个哈希值:

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

JS小提琴演示

顺便提一下,您可以使用散列(URL中#之后的部分)跳转到任何具有id元素,您不需要使用named-anchors( <a name="hi">...</a> )。

要么,您可以使用带锚点的URL(mysite.com/#hi),也可以使用javascript:

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

请注意,您应该使用ID,而不是名称。

您可以使用jQuery及其scrollTo插件,然后编写如下内容:

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

它应该工作得很好。

我认为使用锚点会很有用:

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

在同一文档中创建指向“hi Section”的链接:

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

或者,从另一个页面创建指向“hi Section”的链接:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM