2016-03-02 81 views
1

我想知道如何在網址中點擊edito以在菜單中添加包含jquery的散列?上點擊在jquery中添加散列

例檢測此:

mydomain.com/edito 

和改造,以這樣的:

mydomain.com/#edito 
+1

http://stackoverflow.com/questions/13986231/jquery-ajax-adding-hash-in-the-url檢查此網址 – ChiranjeeviIT

回答

-1

試試這個:

location.hash = "bob" 
1

你應該發現點擊鏈接,然後阻止默認行爲並將用途發送到頁面中的所需位置,如果您可以添加目標元素,效果會更好id作爲a標記上的數據屬性。

  1. 使用data-*屬性目標元素id存儲:

    <a href='mydomain.com/edito' data-target-id='edito'>Edito</a> 
    
  2. 捕捉到點擊鏈接a

    $("a").on('click', function(e) { 
        //Your code here 
    }); 
    
  3. 防止加入e.preventDefault()到事件默認行爲。

  4. 使用hash發送用戶到目標元素:

    window.location.hash = $(this).data("target-id"); 
    

全碼:

$("a").on('click', function(e) { 
    e.preventDefault(); 

    window.location.hash = $(this).data("target-id"); 
}); 

希望這有助於。