我在绑定到 Internet Explorer 7 中的 hashchange 事件时遇到了一些问题。所有其他版本的 Internet Explorer - 即。8 & 9 工作没有问题。
我的代码是:
$(window).bind('hashchange', function (e) { alert('hash changed'); });
当 Firefox、IE8、IE9 中 url 的哈希值发生变化时,我会收到警告框,但在 IE7 中,什么也没有发生。
以前有人经历过吗?
我在绑定到 Internet Explorer 7 中的 hashchange 事件时遇到了一些问题。所有其他版本的 Internet Explorer - 即。8 & 9 工作没有问题。
我的代码是:
$(window).bind('hashchange', function (e) { alert('hash changed'); });
当 Firefox、IE8、IE9 中 url 的哈希值发生变化时,我会收到警告框,但在 IE7 中,什么也没有发生。
以前有人经历过吗?
很确定 IE6 和 IE7 本身不支持它。您是否尝试过使用 Ben Alman 的 jquery BBQ 脚本来解决此问题?
[从jQuery - hashchange 事件中复制这个答案]
我刚刚遇到了同样的问题(IE7 中缺少 hashchange 事件)。适合我的目的的解决方法是绑定散列更改链接的点击事件。
<a class='hash-changer' href='#foo'>Foo</a>
<script type='text/javascript'>
if (("onhashchange" in window) && !($.browser.msie)) {
//modern browsers
$(window).bind('hashchange', function() {
var hash = window.location.hash.replace(/^#/,'');
//do whatever you need with the hash
});
} else {
//IE and browsers that don't support hashchange
$('a.hash-changer').bind('click', function() {
var hash = $(this).attr('href').replace(/^#/,'');
//do whatever you need with the hash
});
}
</script>