【发布时间】:2010-12-28 16:25:17
【问题描述】:
我有以下代码:
$('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标签而不重新加载页面以防止跳转效果?
【问题讨论】:
-
我在当前页面上运行了以下命令,它完全符合您的要求(无需重新加载页面): $("a").click(function(event){event.preventDefault(); window.location.hash = $(this).attr('href');})。也许在您的代码运行时,页面还没有加载?检查
$('ul.questions li a')中有多少项目
标签: jquery hash reload fragment-identifier window.location