2011-07-09 47 views
3

我很慚愧地說我有錨點問題。html錨點只能工作一次

所以我有這樣的代碼:

<a name="map"></a> 

$("div.store_list").click(function() {   
    //do some stuff   
    location.href = location.href + '#map' 
}); 

在做第一個點擊它工作正常。和URL修改:

http://mydomain.local/stores#map 

其次單擊該URL更改爲以下並不起作用:

http://mydomain.local/stores#map#map 

有什麼建議?謝謝

回答

2

問題是解決了使用再次跳,這對我有效:

onclick="location.hash=''; location.hash='#map';" 
4

嘗試location.hash而不是,例如,

location.hash='#map' 
+0

這隻會工作一次。如果再次滾動並調用點擊功能,錨定將不起作用。通過簡單的解決... document.location =「#map」; –

0

您可以檢查以確保網址沒有包含的價值要追加:document.location = "#map";

2

如果您滾動和需要:

$("div.store_list").click(function() {   
    //do some stuff 
    if (location.href.indexOf('#map') == -1) {   
     location.href += '#map'; 
    } 
}); 
0

被接受的解決方案並沒有爲我悲傷地工作。我設法通過將錨值設置爲「#」,然後將其設置到我想要的位置,從而在Chrome中運行。

document.location.href = "#"; 
document.location.href = "#myAnchor"; 

一旦我這樣做了,每當我點擊一個帶有錨標記的鏈接時,它就會被觸發。

$(document).on('click', '.jump_to_instance', e => { 

    // Get anchor name 
    var anchorName = $(e.target).attr("href"); 

    // Set anchor to nothing first 
    document.location.href = "#"; 

    // Set to new anchor value 
    document.location.href = anchorName; 
}); 

<a href="#myAnchor">Click me to go to anchor, I should work multiple times</a> 

<div id="myAnchor">Will jump to here</div>