javascript Firefox 中的“事件”等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9636400/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:17:52  来源:igfitidea点击:

'event' equivalent in firefox

javascriptjqueryfirefoxjavascript-eventsfullcalendar

提问by Adil Malik

I am using the following code and it works perfectly fine in Chrome.

我正在使用以下代码,它在 Chrome 中运行良好。

function dayBind(xyzValue) {
    if(event.type == 'click')
       alert('Mouse Clicked')
}

Note that there was no 'event' variable passed to the function but still it was available for me in case of chrome. But when I use firefox I get 'event' undefined. I tried using the following workarounds:

请注意,没有传递给函数的“事件”变量,但在 chrome 的情况下它仍然可供我使用。但是当我使用 Firefox 时,我得到了未定义的“事件”。我尝试使用以下解决方法:

var e=arguments[0] || event;

also:

还:

var e=window.event || event;

But none of them worked for me. Is there any 'event' equivalent in Firefox?

但他们都没有对我来说有效。Firefox 中是否有任何“事件”等价物?

回答by xdazz

Because IE and Chrome put the event in the global object window, so you can get it. In firefox, you need to let the first parameter be the event.

因为 IE 和 Chrome 把事件放在全局对象中window,所以你可以得到它。在 Firefox 中,您需要让第一个参数成为事件。

function dayBind(event, xyzValue) {
    var e=event || window.event;
    if(event.type == 'click')
       alert('Mouse Clicked')
}

回答by Pointy

If you're setting up the handler with an "onclick" attribute or something (which, since you tagged the question "jQuery", you really should consider not doing), you have to explicitly pass it:

如果您使用“onclick”属性或其他属性设置处理程序(因为您标记了问题“jQuery”,您确实应该考虑不这样做),您必须明确传递它:

<button type=button onclick='whatever(event)'>Click Me</button>

回答by Paul Sweatte

If you need it to work cross browser, simply use the argumentsobject:

如果您需要它跨浏览器工作,只需使用该arguments对象:

function dayBind() 
  {
  var e=arguments[0];

  if(!!e && e.type === 'click')
    {
    alert('Mouse Clicked') 
    }
  }

References

参考

回答by Bocil Squarpant

I am working in a plugin's callback function. I cannot call it myself.

我正在使用插件的回调函数。我自己不能称之为。

One simple question to your suggestion: when you write: onclick="whatever(event)"you are writing javascript in the value of onclickattribute, right?

对您的建议提出一个简单的问题:当您编写时:onclick="whatever(event)"您是在onclick属性值中编写 javascript ,对吗?

Why can't you make the same function call inside some other function like this:

为什么不能在其他函数中进行相同的函数调用,如下所示:

function foo(){ whatever(event); // this is also javascript }

function foo(){ whatever(event); // this is also javascript }

// But this doesn't work for me in FireFox 10.0.2

// 但这在 FireFox 10.0.2 中对我不起作用

The code in the "onclick" attribute should be thought of as part of a function that the browser creates for you. That function automatically has the "event" parameter available. Writing the attribute as I did in the answer cause that parameter to be passed on the your other function.

“onclick”属性中的代码应该被视为浏览器为您创建的函数的一部分。该函数自动具有可用的“事件”参数。像我在答案中所做的那样编写属性会导致该参数传递给您的另一个函数。

Really, you should read about the jQuery API and use that to bind event handlers instead of using "onclick" and other similar attributes.

真的,您应该阅读 jQuery API 并使用它来绑定事件处理程序,而不是使用“onclick”和其他类似的属性。