[英]Image load event in IE8; event is undefined and this is `window` object
I have written the below code and it is working in all modern browsers except in <IE9
我已经写了下面的代码,它在除
<IE9
之外的所有现代浏览器中都可以使用
<script>
function eventH(event) {
alert(this);
}
</script>
<img src="h300.png" onload="eventH(arguments[0]);"/>
It fires event handler, but event
is undefined and this
value is parent object. 它触发事件处理程序,但
event
未定义,并且this
值是父对象。 Why? 为什么?
Does IE handle this differently or I am wrong somewhere in understanding this? IE是否以不同方式处理此问题,或者我在理解这一点上错了?
Internet Explorer 8 and below do not implement the W3C event model. Internet Explorer 8及以下版本未实现W3C事件模型。 For IE, what you need might instead be part of
window.event
. 对于IE,您需要的可能是
window.event
一部分。
QuirksMode gives a good set of examples of how to write event-handling code compatible with both modern browsers and old IE versions. QuirksMode提供了很多示例 ,说明如何编写与现代浏览器和旧IE版本兼容的事件处理代码。
Also note that when you call a JavaScript function without using dot notation, the global window object is this
(except in ES5 strict mode, in which accessing this
is not allowed in this case). 还要注意,当您在不使用点符号的情况下调用JavaScript函数时,全局窗口对象就是
this
(在ES5严格模式下,在这种情况下不允许访问this
对象)。 Instead, you can pass this
to your function as an argument. 相反,您可以
this
作为参数传递给函数。
However, it may be better to just attach your event handler using .addEventListener() (W3C model) or .attachEvent() (Microsoft model) instead of specifying it in the HTML. 但是,最好使用.addEventListener() (W3C模型)或.attachEvent() (Microsoft模型)附加事件处理程序,而不是在HTML中指定它。 Then,
this
will work as expected. 然后,
this
将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.