[英]Firefox “window.event is undefined” error
I have this script: 我有这个脚本:
function postBackByObject(e) {
var o = window.event.srcElement || e.target;
if (o.tagName == "INPUT" && o.type == "checkbox") {
__doPostBack("", "");
}
}
I use this script with onclick="postBackByObject();"
我将此脚本与
onclick="postBackByObject();"
. 。
but in Firefox 21 I get this error: 但是在Firefox 21中,我收到此错误:
TypeError: window.event is undefined
TypeError:window.event未定义
what is my wrong? 我怎么了
That's because it is. 那是因为。
window.event
is for older versions of IE. window.event
适用于旧版本的IE。
The typical way to do this is: 典型的方法是:
function postBackByObject(e) {
e = e || window.event;
var o = e.srcElement || e.target;
// ...
}
You are attaching events inline onclick="postBackByObject();"
您将内联事件
onclick="postBackByObject();"
Try passing this
(the event target) to onclick="postBackByObject(this);"
尝试
this
(事件目标)传递给onclick="postBackByObject(this);"
Modify your function to handle this change: 修改您的函数以处理此更改:
function postBackByObject(e) {
if (e.tagName == "INPUT" && e.type == "checkbox") {
__doPostBack("", "");
}
}
A better alternative will be to attach events using addEventListener
更好的选择是使用
addEventListener
附加事件
If your markup looks like: 如果您的标记看起来像:
<div id="TvCategories" onclick="postBackByObject(this);" />
then 然后
document.getElementById('TvCategories').addEventListener('click', postBackByObject);
Your postBackByObject
function remains unchanged when using this approach. 使用此方法时,您的
postBackByObject
函数保持不变。
Pass the event on the onClick
event like this: 将事件传递给
onClick
事件,如下所示:
onclick="myCustomFunction(event);"
It does work and you can access the event object! 它确实有效,您可以访问事件对象!
It's possible to pass event object in the inline declaration: 可以在内联声明中传递事件对象:
<input type="button" onclick="postBackByObject(event);" value="click" />
function postBackByObject(e) {
var o = e.srcElement || e.target;
// ...
}
Attaching the event is a solution but there's an alternative one that could be helpful for other people facing the same problem, after a long search I found out that: 附加事件是一种解决方案,但是有另一种选择可能对其他面临相同问题的人有所帮助,经过长时间的搜索,我发现:
Event object is only reconized by firefox if you send explicitly the 'event' from the function
仅当您从函数显式发送“事件”时,才由firefox协调事件对象
So the problem occurs because window.event
is not recognized by Firefox, and the solution is to pass event
to the function, your code will be : 因此出现问题是因为Firefox无法识别
window.event
,而解决方案是将event
传递给函数,您的代码将是:
function postBackByObject(e) {
var o = e.srcElement || e.target;
if (o.tagName === "INPUT" && o.type === "checkbox") {
__doPostBack("", "");
}
}
And you can still call it in your inline HTML passing event
as a parameter: 您仍然可以在内联HTML传递
event
中将其作为参数调用:
onclick="postBackByObject(event);"
Your line 你的线
var o = window.event.srcElement || e.target;
fails on all browsers excepting IE, since for them windows.event
is undefned 在除IE之外的所有浏览器上均失败,因为对于它们来说,
windows.event
是未定义
The proper formulation would be : 正确的公式为:
var o = e ? e.target : window.event.srcElement;
Since standard compliant browsers will pass the event as the parameter e
and the target at e.target
in IE, e
will be undefined and you must use window.event.srcElement
由于符合标准的浏览器会将事件作为参数
e
传递给目标,并将目标作为IE中的e.target
传递,因此e
将是未定义的,因此必须使用window.event.srcElement
Note that recent versions of IE do support the standards compliant model. 请注意,最新版本的IE确实支持符合标准的模型。
On a more generic note, when you try and access a value as abcd
then abc
must be a defined object else you will get the error abc undefined
. 更一般地说,当您尝试访问
abcd
值时, abc
必须是已定义的对象,否则会得到错误abc undefined
。
i am not sure if window.event is supported in all browsers. 我不确定所有浏览器是否都支持window.event。 I think you get event in variable "e" of postBackByObject(e)
我认为您在postBackByObject(e)的变量“ e”中获得了事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.