jquery - 在 jQuery 中更改哈希值而无需重新加载

标签 jquery hash reload fragment-identifier window.location

我有以下代码:

$('ul.questions li a').click(function(event) {
    $('.tab').hide();
    $($(this).attr('href')).fadeIn('slow');
    event.preventDefault();
    window.location.hash = $(this).attr('href');
});

这只是根据您单击的时间淡入 div,但我希望页面 URL 哈希标记在您单击时更改,以便人们可以复制它并为其添加书签。目前,当哈希标签更改时,这会有效地重新加载页面。

是否可以通过更改hash标签而不重新加载页面来防止跳转效果?

最佳答案

这对我有用

$('ul.questions li a').click(function(event) {
    event.preventDefault();
    $('.tab').hide();
    window.location.hash = this.hash;
    $($(this).attr('href')).fadeIn('slow');
});

查看此处http://jsbin.com/edicu具有几乎相同代码的演示

关于jquery - 在 jQuery 中更改哈希值而无需重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1939041/

相关文章:

python - 无法重新加载位于另一个文件夹中但也在 sys.path 中的模块

javascript - location.reload(true) 已弃用

javascript - CSS按钮动画不起作用

r - 如何以在 R 中生成人类可读输出的方式匿名化向量?

php - PHP crypt() 的输出长度是多少?

php - VB.NET 密码哈希函数的 PHP 等效项

java - Android Java 重新加载刷新 Activity fragment

javascript - 来自服务器 json 对象的数据表

javascript - 获取错误 "ReferenceError: event is not defined"

javascript - 使用jQuery从字符串中获取倒数第二个字符位置