'event' equivalent in firefox

非 Y 不嫁゛ 提交于 2019-11-27 09:31:10

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.

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

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:

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

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

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

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

References

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 onclick attribute, right?

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

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

// But this doesn't work for me in 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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!