如何添加#hash单击元素

mar*_*zzz 8 javascript url hash jquery

当我在http://www.mywebsite.com/打开我的页面时,我有这个jQuery代码:

$('#locSlideButton2').click(function() {

});
Run Code Online (Sandbox Code Playgroud)

我想,点击locSlideButton2元素,在网址中添加一个哈希(例如#example),而不进行任何重定向.

我该怎么做?

Som*_*Guy 45

使用普通的老香草JS:

window.location.hash='example';
Run Code Online (Sandbox Code Playgroud)

关于window.location的MDN


ade*_*neo 7

有两种方法,要么你使用javascript,你可以访问window.location.hash,或者你将你的点击事件绑定到一个<a href="#example">,并防止点击默认,或者当你的页面达到顶部时认为它很酷,并且哈希值应出现在浏览器地址栏中.