简体   繁体   中英

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.

     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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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