在IE中出错时手动调用事件处理程序,给定一定条件 [英] Error in IE when manually calling event handler, given certain conditions

查看:89
本文介绍了在IE中出错时手动调用事件处理程序,给定一定条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 请注意,我不是在寻找代码解决方案,而是深入了解为什么会发生这种情况。 li>
  • 错误发生在IE(测试7和8),但不是Firefox,Chrome,Safari。

当手动调用分配给 onclick 的功能时,IE会抛出一个错误:对象不支持此操作如果满足以下所有条件

When manually calling a function assigned to onclick, IE with throw a Error: Object doesn't support this action if all of the following conditions are met:


  1. 您可以直接通过元素在[event] 属性中调用该方法。

  2. 您不使用 .call() .apply()

  3. 你传递一个参数参数,即使未定义)。

  4. 您尝试将返回值分配给变量。

  1. You call the method directly via the element's on[event] property.
  2. You do not use .call() or .apply().
  3. You pass an argument (any argument, even undefined).
  4. You attempt to assign the return value to a variable.

违反任何一个规则,并且调用成功。

Violate any one of those rules, and the call succeeds.

该函数本身似乎没有任何作用用它。

The function itself appears to have nothing to do with it. An empty function gives the same result.

var elem = document.getElementById('test');  // simple div element.
var result;               // store result returned.

function test_func(){};   // function declaration.
                          // function expression behaves identically.

elem.onclick = test_func; // assign test_func to element's onclick.

// DIRECT CALL
test_func();                 // works
test_func( true );           // works
result = test_func();        // works
result = test_func( true );  // works

// DIRECT CALL, CHANGING THE CONTEXT TO THE ELEMENT
test_func.call( elem );                  // works
test_func.call( elem, true );            // works
result = test_func.call( elem );         // works
result = test_func.call( elem, true );   // works ******** (surprising)

// CALL VIA ELEMENT, USING .call() METHOD, CHANGING THE CONTEXT TO THE ELEMENT
elem.onclick.call( elem );                  // works
elem.onclick.call( elem, true );            // works
result = elem.onclick.call( elem );         // works
result = elem.onclick.call( elem, true );   // works ******** ( very surprising)

// CALL VIA ELEMENT
elem.onclick();                 // works
elem.onclick( true );           // works
result = elem.onclick();        // works
result = elem.onclick( true );  // Error: Object doesn't support this action



摘要



再次,我不需要代码解决方案。相反,如果有人深入了解为什么IE以这种方式实现,我很好奇。

Summary

Again, I don't need a code solution. Rather I'm curious if anyone has insight into why IE is implemented this way.

非常感谢。

编辑:为了澄清一件事情,实际功能什么也没有任何区别。命名参数,不是命名参数,返回参数,返回一个文字值,返回未定义,所有这些都不起作用。

To clarify one thing, nothing with the actual function seems to make any difference. Naming parameters, not naming them, returning the argument, returning a literal value, returning undefined, all of these have no effect.

这可能是因为函数似乎从不实际上叫。正如我在下面的评论中指出的,导致此调用的代码运行正常,因此它也不是解析问题。但是当解释器到达这个时,它会看到:

This is likely because the function seems to never actually get called. As I noted in a comment below, the code leading up to this call runs fine, so it isn't a parsing issue either. But when the interpreter gets to this one, it sees:


变量+ AssignmentOperator + DOMElement + EventHandler + CallOperator + Argument

Variable + AssignmentOperator + DOMElement + EventHandler + CallOperator + Argument

...并抛出错误。没有操纵我似乎有任何区别。有效的删除任何一个,并且错误消失。

...and throws the Error. No manipulation I do seems to make any difference. A valid removal of any one of those, and the Error disappears.

如果我将一个变量添加到存储处理程序的中间,然后将其从

If I place add a variable to the middle of it that stores the handler, then fire it from the variable it works.

var temp = elem.onclick;
result = temp( true );    // works

...但这不应该是一个惊喜,因为它是有效的与上述第四个版本相同。

...but this shouldn't be much of a surprise, since it is effectively the same as the fourth version above.

推荐答案

至于为什么它是以这种方式实现的,可能没有答案外。一个很好的例子是,当前IE开发人员innerHTML的发明者面临着问题 with innerHTML本身。

As to "why" it was implemented this way, there's probably no answer from the outside. A good example is when former IE developer, the inventor of innerHTML, faces problems with innerHTML itself.

请问为什么也是不必要的,因为

Asking why is also unnecessary because


  1. 不要经常使用参数调用事件处理程序

  2. 您可以解决问题(正如您在问题中所述)

另外要注意的是,你的比喻太具体了。该问题不仅限于 赋值表达式 ,您可以使用其他类型的 表达式 code> :

Another thing to note is that your analogy is too specific. The issue is not restricted to the assignment expression, you can reproduce it with other types of expressions:

undefined === elem.onclick( true )
typeof elem.onclick( true )
elem.onclick( true ) - 1
alert(elem.onclick( true ))

这篇关于在IE中出错时手动调用事件处理程序,给定一定条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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