我在页面上有这样的元素
<a href="#hi">go to hi</a>
.
.
<a name="hi">hi text here</a>
Run Code Online (Sandbox Code Playgroud)
但我希望用户首先在页面加载时转到"hi text here".这该怎么做?
在移动用户的浏览器以关注另一个元素之前,我建议您首先测试另一个哈希值:
if (!document.location.hash){
document.location.hash = 'hi';
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,您可以使用散列(#URL中的部分)跳转到任何具有的元素id,您不需要使用named-anchors(<a name="hi">...</a>).
您可以使用带有锚点的 URL (mysite.com/#hi),也可以使用 javascript:
document.getElementById('hi').scrollIntoView(true);
Run Code Online (Sandbox Code Playgroud)
请注意,您应该使用 ID,而不是姓名。
| 归档时间: |
|
| 查看次数: |
8935 次 |
| 最近记录: |