繁体   English   中英

使用JavaScript在页面正文中显示当前页面的哈希/锚点

[英]Display the current page's hash/anchor in the page's body with JavaScript

假设用户访问页面http://somedomain.com/path/file.html#foo ,我想使用JavaScript在页面上的某处显示以下文本:

 → foo
  1. 使用window.location.hash获取锚点的名称。

     var anchorName = window.location.hash; 
  2. 创建一个文本节点以显示所需内容:

     var path = document.createTextNode("→ " + anchorName); 
  3. 将其添加到Dom中:

     document.getElementById(idOfContainerYouWantToEdit).appendChild(path); 

暂无
暂无

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

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