为什么jQuery会重启事件两次? [英] Why does the jQuery resize event fire twice?

查看:101
本文介绍了为什么jQuery会重启事件两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚注意到,只要调整浏览器窗口的大小,jquery resize事件就会触发两次。我找不到任何将此列为默认行为的文档。我已经使用jQuery 1.4和1.5在最新版本的Chrome和Firefox中对此进行了测试。

I just noticed that the jquery resize event fires twice any time the browser window is resized. I'm unable to find any documentation listing this as the default behavior. I have tested this in the latest versions of Chrome and Firefox, using jQuery 1.4 and 1.5.

有没有其他人看到这种行为?是否有特定原因?

Does anyone else see this behavior? Is there a specific reason for it?

更新:如果我单击浏览器窗口上的最大化按钮一次,就会发生这种情况。我知道如果我手动将窗口拖动到不同的大小,事件将被多次触发,但最大化按钮不应该是这种情况。

Update: This happens if I click the maximize button on the browser window once. I understand that if I'm manually dragging the window to a different size, the event will be fired multiple times, but that shouldn't be the case with the maximize button.

使用的代码测试如下。浏览器调整大小的文本输出两次:

The code used test this is below. The browser resized text is output twice:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<script type="text/javascript">
    $(window).resize(function(){
        console.log('Browser Resized');
    });
</script>
<body>

</body>
</html>


推荐答案

这不是jQuery的问题。这取决于浏览器的实现。浏览器决定何时触发'调整大小'。您可以使用window.onresize = function(){...}进行测试。它会是一样的。与jQuery无关。我测试了Win7 chrome,它被激发了两次,而在Win7 opera / safari中,它被触发了一次。

This is not a problem of jQuery. It depends on the browser implementation. The browser decides when to trigger 'resize'. And you can use window.onresize = function() {...} to test. It will be same. Nothing to do with jQuery. I tested on Win7 chrome, it is fired twice, and in Win7 opera/safari it's fired once.

这篇关于为什么jQuery会重启事件两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆