开发者

How can I execute EventListener 'click' from JavaScript

I have event listener myObject.addEventListener('click',this.doSomething,false) on element. It works fine when I do mouse click but I cannot figure out how to trigger click from JavaScript. It seems like element.click() does not work for divs?

I am using jQuery and I have also tried trigger('click') but nothing is happening.

How can I in JavaScript 开发者_如何学Goexecute the EventListener?

Update:

Here is sample code http://jsbin.com/iniwi5/2


Can you bind the event using jQuery instead?

$(myobject).click(function() {

});

Then

myobject.trigger('click');

would programmatically trigger the click.


To trigger a click event listener added via addEventListener, you need to manually dispatch a click event, using either dispatchEvent (for standards-compliant browsers) or fireEvent (for IE < 9).


Try this:

myObject.click; // no `()`

OR:

(myObject.click)();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜