javascript Firefox“window.event 未定义”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17295901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firefox "window.event is undefined" error
提问by Majid
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
类型错误:window.event 未定义
what is my wrong?
我怎么了?
回答by Niet the Dark Absol
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;
// ...
}
回答by c.P.u1
You are attaching events inline onclick="postBackByObject();"
您正在内联附加事件 onclick="postBackByObject();"
Try passing this
(the event target) to onclick="postBackByObject(this);"
Modify your function to handle this change:
尝试通过this
(事件目标)onclick="postBackByObject(this);"
修改您的函数以处理此更改:
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
使用此方法时,您的功能保持不变。
回答by Deian Motov
Pass the event on the onClick
event like this:
onClick
像这样在事件上传递事件:
onclick="myCustomFunction(event);"
onclick="myCustomFunction(event);"
It does work and you can access the event object!
它确实有效,您可以访问事件对象!
回答by cн?dk
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 :
所以出现问题是因为window.event
Firefox无法识别,解决方法是传递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:
您仍然可以在event
作为参数传递的内联 HTML 中调用它:
onclick="postBackByObject(event);"
回答by Dmitry
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;
// ...
}
回答by HBP
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
,而目标e.target
在 IE 中,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 a.b.c.d
then a.b.c
must
be a defined object else you will get the error a.b.c undefined
.
在更通用的说明中,当您尝试访问一个值时,因为a.b.c.d
thena.b.c
必须是一个已定义的对象,否则您将收到错误a.b.c undefined
。
回答by amit
i am not sure if window.event is supported in all browsers. I think you get event in variable "e"of postBackByObject(e)
我不确定是否所有浏览器都支持 window.event。我认为你在postBackByObject(e) 的变量“e”中得到事件