我正在尝试使用#hash值重新加载页面,以便在页面刷新时获取哈希值并吐出一个函数。这适用于除IE的所有版本之外的所有浏览器。当我点击链接时,它会添加#value,但在重新加载后会显示#undefined。
知道可能导致这种情况的原因吗?
注意:
//refreshes the page when the #by-date-link is clicked
$('.sort-trigger').on('click', function(e){
e.preventDefault();
var urlHash = $(this).attr('href');
window.location.hash = urlHash;
window.location.reload();
});
答案 0 :(得分:0)
为什么重装?另外,我假设href attr是#something - 这是一个哈希。转储location.hash并使用onhashchange是不是更有意义?
编辑:没有正确阅读。我的猜测是,preventDefault与单击锚定链接相结合会给IE带来问题,所以可能会将hash存储在localstorage(或类似的东西)并将其设置为onload?
还发现了这个:
if(event.preventDefault){ event.preventDefault(); } else { event.returnValue = false; }