如果没有一个有效的例子,我相信问题就在这一行:
window.addEventListener("hashchange", locationChange(), false);
添加侦听器是正确的操作过程,但是在定义侦听器时调用的是函数。改变
locationChange()
locationChange
不会立即调用函数,而是向函数传递句柄:
function locationChange() {
if(location.hash == "#1") {
$('#list-item-1').css('background-color', '#333333');
$('#list-item-2').css('background-color', '#333333');
} else {
$('#list-item-1').css('background-color', '#ffffff');
$('#list-item-2').css('background-color', '#ffffff');
}
);
window.addEventListener("hashchange", locationChange, false);