例如,如果一个用户在 http://example.com 上,然后用户前往 http://example.com#comments。如果用户在浏览器上点击“后退”按钮,我如何使他“忽略”http://example.com 并直接返回到之前访问的URL?
我已经加载了 jQuery。
我已经加载了 jQuery。
而不是像这样有一个链接:
<a href='#comments'>Link</a>
使用 location.replace()
可以在浏览器历史记录中“覆盖”http://example.com
的记录。
https://developer.mozilla.org/en/DOM/window.location
示例:
HTML:
<a id='commentsLink'>Link</a>
JavaScript:
$("#commentsLink").click(function(){
window.location.replace("#comments");
});
window.location
信息的替代链接:https://developer.mozilla.org/en/DOM/window.location) - Platinum Azure
#comments
的那个事件,可能需要使用preventDefault()
函数,并使用jQuery来滚动到该位置。否则,这一切都将成为正常历史记录的一部分。 - Sparky