IE7 中绑定 hashchange 事件问题

发布于 2024-11-26 22:41:36 字数 300 浏览 1 评论 0原文

我遇到一些与 Internet Explorer 7 中的 hashchange 事件绑定的问题。所有其他版本的 Internet Explorer - 即。 8& 9 工作没有问题。

我的代码是:

 $(window).bind('hashchange', function (e) { alert('hash changed'); });

当 Firefox、IE8、IE9 中 url 的哈希发生变化时,我会收到警报框,但在 IE7 中,没有任何反应。

有人经历过这个吗?

I am experiencing some issues binding to the hashchange event in Internet Explorer 7. All other versions of Internet Explorer - ie. 8 & 9 work without issue.

My code is:

 $(window).bind('hashchange', function (e) { alert('hash changed'); });

When the hash of the url changes in Firefox, IE8, IE9 I get the alert box, but in IE7, nothing happens.

Anyone experience this before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光飞翔i 2024-12-03 22:41:36

很确定 IE6 和 IE7 本身不支持它。您是否尝试使用 Ben Alman 的 jquery BBQ 脚本来解决此问题?

Pretty sure IE6 and IE7 don't support it natively. Did you try using Ben Alman's jquery BBQ script which fixes this?

忘东忘西忘不掉你 2024-12-03 22:41:36

[从 jQuery - hashchange event 复制这个答案]

我刚刚遇到了同样的问题(缺乏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>

[Copying this answer from jQuery - hashchange event ]

I just ran into the same problem (lack of hashchange event in IE7). A workaround that suited for my purposes was to bind the click event of the hash-changing links.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文