简体   繁体   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. Use window.location.hash to grab the anchor's name. 使用window.location.hash获取锚点的名称。

     var anchorName = window.location.hash; 
  2. Create a text node to display what you want: 创建一个文本节点以显示所需内容:

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

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

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

相关问题 从当前页面的网址中删除哈希 - Remove hash from current page’s URL Javascript:找出当前页面的语言环境 - Javascript: find out current page's locale 在javascript中提取部分当前页面的URL - Extracting part of current page's URL in javascript 从AJAX版本页面的body元素中提取所有类,并替换当前页面的body类 - Extract all classes from body element of AJAX-ed page and replace current page's body classes JavaScript:转到另一个页面以获取不在当前页面上的特定信息? - JavaScript: Travel to another page to get specific info that's not on the current page? 从页面的 url 中删除页面的锚点链接 - to delete a page's anchor's link from page's url 从当前页面的位置解析锚点的最简单方法是什么? - What's the easiest way to parse the anchor out of the current page's location? 了解(通过 JavaScript)是否使用浏览器的历史记录到达当前页面 - Understand (via JavaScript) if the current page is reached using browser's history Chrome 扩展程序将外部 javascript 添加到当前页面的 html - Chrome extension adding external javascript to current page's html 在Javascript中,如何获取gridview的当前页面索引? - In Javascript, how do I obtain a gridview's current page index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM