2012-06-12 51 views
2

假設一個用戶訪問的頁面http://somedomain.com/path/file.html#foo,我想顯示,使用JavaScript,某處的頁面上的文本,如:顯示在頁面是用JavaScript體當前頁面的哈希/錨

→ foo 
+0

你的意思是你自己的網頁,並希望該行爲爲每個用戶,或者你想瀏覽器擴展做這樣的事情? – haylem

+0

我自己的網頁,並希望它顯示# – UndergroundVault

+0

好的後。然後看到我的回答,我冒昧地重寫了一下你的問題。 – haylem

回答

4
  1. 使用window.location.hash來獲取錨點的名稱。

    var anchorName = window.location.hash; 
    
  2. 創建一個文本節點,以顯示你想要什麼:

    var path = document.createTextNode("→ " + anchorName); 
    
  3. 它添加到DOM:

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