在页面加载时自动跳转到元素 [英] Jump to a element automatically on page load

查看:332
本文介绍了在页面加载时自动跳转到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有这样的元素

i have elements like this on page

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

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

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

推荐答案

我建议您首先测试另一个哈希值,然后再移动用户的浏览器以关注另一个哈希值element:

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小提琴演示

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

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

这篇关于在页面加载时自动跳转到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆