出于某种原因,此 JavaScript 不会将单击的元素的 ID 添加到地址栏中。它将正确执行其他所有操作。
这是 jsfiddle: http://jsfiddle.net/4x7La64s/
例如,如果我点击#introduction
,我希望它将ID放入地址栏中,如下所示:example.com/page.php/#introduction
最佳答案
基于您的代码,完成后:
e.preventDefault();
你可以这样做:
var hash = jQuery(this).attr("href"); //Your clicked link
if (window.history && window.history.pushState) {
history.pushState(null, null, 'hash'); // This won't make the page jump to the id
} else {
location.hash = hash; //This will be the fallback for old browsers and this will make the page jump to that id
}
关于JavaScript 未在地址栏中添加 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31302795/