开发者

How can I call mouseover event handler programmatically in JavaScript

开发者 https://www.devze.com 2023-01-02 21:49 出处:网络
In my html, I have an html element with a mouseover event handler. Can you please tell me if it is possible开发者_开发技巧 for me to invoke that event handler programmically in JavaScript?It is possib

In my html, I have an html element with a mouseover event handler. Can you please tell me if it is possible开发者_开发技巧 for me to invoke that event handler programmically in JavaScript?


It is possible. Here's a cross browser function to fire an event:

function eventFire(el, etype){
    if (el.fireEvent) {
      el.fireEvent('on' + etype);
    } else {
      var evObj = document.createEvent('Events');
      evObj.initEvent(etype, true, false);
      el.dispatchEvent(evObj);
    }
}
// => exmaples
// => eventFire(myDiv,'mouseover');
// => eventFire(myButton,'click');


You can use the fireEvent method available for IE. I am not sure if this will work for FF or other browsers. you can simply fire the event by

buttonObject.fireEvent('onclick');

For more details have a look at MSDN.

0

精彩评论

暂无评论...
验证码 换一张
取 消