Firefox 4 新点击事件行为的问题

发布于 2024-11-11 13:41:48 字数 1048 浏览 3 评论 0原文

我对 Firefox 4 的行为存在疑问,即传递给单击事件时调用的函数的参数。

看一下这个例子:

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.11/mootools.js"></script>
</head>
<body>
    <span id="e">Klick mich!</span> 
    <script type="text/javascript">
        $("e").addEvent("click", function(a, b, c){
            alert(this);
            alert(a);
            alert(b);
            alert(c);
            console.log(this);
            console.log(a);
            console.log(b);
            console.log(c);
        }.bind(1, [2, 3]));
    </script>   
</body>
</html>

如果你用 Firefox 4 打开这个,结果是:

  1. 1
  2. 2,3
  3. object MouseEvent
  4. undefined

在任何其他浏览器中,结果是:

  1. 1
  2. 2
  3. 3
  4. undefined

正如你所看到的,只有 Firefox 4 将 MouseEvent 传递给函数。这种行为破坏了我的很多代码。

你知道有什么解决办法吗?感谢您的帮助。

EDIT1:Chrome 的行为类似于 FF4

I have a problem with the behavior of Firefox 4 in regards to parameters passed to the function that is getting called upon a click event.

Take a look at this example:

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.11/mootools.js"></script>
</head>
<body>
    <span id="e">Klick mich!</span> 
    <script type="text/javascript">
        $("e").addEvent("click", function(a, b, c){
            alert(this);
            alert(a);
            alert(b);
            alert(c);
            console.log(this);
            console.log(a);
            console.log(b);
            console.log(c);
        }.bind(1, [2, 3]));
    </script>   
</body>
</html>

If you open this with Firefox 4 the result is:

  1. 1
  2. 2,3
  3. object MouseEvent
  4. undefined

In any other browser the result is:

  1. 1
  2. 2
  3. 3
  4. undefined

As you can see only Firefox 4 passes an MouseEvent to the function. This behavior breaks a lot of my code.

Do you know any solution? Thanks for help.

EDIT1: Chrome behaves like FF4

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

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

发布评论

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

评论(1

如何视而不见 2024-11-18 13:41:48

问题是 - 这是 mootools 1.11 -

mootools 1.11 中不支持且旧,它被接受使用( http://docs111.mootools.net/Native/Function.js#Function.bind ):

bind 可选,函数的“this”将引用的对象。

args 可选,传递的参数。如果参数 > 则必须是数组1

因此,执行 .bind(1, [args]) 是正确的。然而,最近原生 Function.bind 实现在实现它的浏览器中发生了变化 - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

thisArg 调用绑定函数时作为 this 参数传递给目标函数的值。如果使用 new 运算符构造绑定函数,则该值将被忽略。

arg1, arg2, ... 调用目标函数时添加到提供给绑定函数的参数前面的参数。

这意味着,要使其工作,您需要 .bind(1,2,3,4); 其中 1 是绑定范围,2,3,4 是参数。

应该升级,在 Mootools 1.11 编写 4 年后的浏览器上运行它会产生不可预测的结果。总是。例如,1.11 将不再检测 gecko/ff,因为用于测试它的 func 已弃用。

接下来做什么,检查 netscape4? :)

the problem is - this is mootools 1.11 - UNSUPPORTED AND OLD

in mootools 1.11, it was accepted to use ( http://docs111.mootools.net/Native/Function.js#Function.bind ):

bind optional, the object that the “this” of the function will refer to.

args optional, the arguments passed. must be an array if arguments > 1

hence, doing .bind(1, [args]) was correct. However, recently the native Function.bind implementation changed in browsers that implement it - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

thisArg The value to be passed as the this parameter to the target function when the bound function is called. The value is ignored if the bound function is constructed using the new operator.

arg1, arg2, ... Arguments to prepend to arguments provided to the bound function when invoking the target function.

it means, to make it work you need .bind(1,2,3,4); where 1 is bound scope and 2,3,4 are arguments.

you SHOULD upgrade, running mootools 1.11 on browsers that came 4 years after it got written will yield unpredictable results. always. for example, 1.11 won't detect gecko/ff anymore due to deprecated func used to test for it.

what next, check for netscape4? :)

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