簡體   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