admin管理员组文章数量:1319957
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 execute the EventListener?
Update:
Here is sample code
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 execute the EventListener?
Update:
Here is sample code http://jsbin./iniwi5/2
Share Improve this question edited Jun 23, 2011 at 20:40 jpkeisala asked Jun 23, 2011 at 20:22 jpkeisalajpkeisala 8,9668 gold badges32 silver badges40 bronze badges 3- There are tons of exact duplicates, e.g. how can I trigger a JavaScript event click, JavaScript: Invoking click-event of an anchor tag from javascript, etc. – maerics Commented Jun 23, 2011 at 20:24
- Neither of those "duplicates" are formulated with event listeners in mind. – zjmiller Commented Jun 23, 2011 at 20:59
- Yes, this is specific for Event Listener, if I was using normal jQuery .click() listener the code works just fine but seems like addEventListener behaves differently. – jpkeisala Commented Jun 26, 2011 at 9:38
3 Answers
Reset to default 5Can 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-pliant browsers) or fireEvent
(for IE < 9).
Try this:
myObject.click; // no `()`
OR:
(myObject.click)();
本文标签: jqueryHow can I execute EventListener 39click39 from JavaScriptStack Overflow
版权声明:本文标题:jquery - How can I execute EventListener 'click' from JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742058221a2418427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论