我的問題是與此類似,但沒有一個答案解決我的問題: Use JQuery preventDefault(), but still add the path to the URLJQuery的preventDefault()方法,但仍然添加片段路徑URL,而不導航到該片段
當用戶點擊一個片段鏈接,我需要刪除跳轉到片段的默認行爲,但仍然將片段添加到URL。此代碼(從鏈接中獲取)將觸發動畫,然後將片段添加到URL中。然後片段被導航到,我的案件打破了我的網站。
$("#login_link").click(function (e) {
e.preventDefault();
$("#login").animate({ 'margin-top': 0 }, 600, 'linear', function(){ window.location.hash = $(this).attr('href'); });
});
因此,只需要清楚,replaceState將允許我將url更改爲mysite.com#標題而不滾動到標題ID爲ID的元素? – Evans 2012-03-20 17:28:04
@jdln是的。有關瀏覽器兼容性,請參閱http://caniuse.com/#feat=history – 2012-03-20 17:36:16