>我有这段代码来切换div的内容,并且需要在URL中有一个哈希值,但是使用document.location.hash = a;
行页面将重新加载。如何在不重新加载页面的情况下设置哈希?
$('.leftPass').click(function() {
var a = $(this).attr('id');
var b = 'chapters/' + a + '.php';
$('#storyR').load(b);
$("html, body").animate({
scrollTop: 0
}, 1000);
$('.leftAct').removeClass('leftAct').addClass('leftPass');
$(this).removeClass('leftPass').addClass('leftAct');
document.location.hash = a;
});
location.hash
是正确的。更改它永远不会重新加载页面。.leftPass
是链接吗?如果是,请在事件处理程序中调用 e.preventDefault()
。 e
应该是函数的第一个参数:
$('.leftPass').click(function(e) {
e.preventDefault();
// ...
});