admin管理员组文章数量:1417107
I need to simulate a click on a button using jquery.
Regular Javascript MouseEvents (mousedown and mouseup) do work, but I want jquery.
I have the div in a jquery object: (just a reference)
it was defined like
var $button = $(document.getElementById("iframe")...);
$button
-> <div id="1" class="2" role = "button"><b>Action</b></div>
I've tried with $button.click()
and $button.trigger('click')
but both don't work.
I've read this, but provides no answer to this question.
I need to simulate a click on a button using jquery.
Regular Javascript MouseEvents (mousedown and mouseup) do work, but I want jquery.
I have the div in a jquery object: (just a reference)
it was defined like
var $button = $(document.getElementById("iframe")...);
$button
-> <div id="1" class="2" role = "button"><b>Action</b></div>
I've tried with $button.click()
and $button.trigger('click')
but both don't work.
I've read this, but provides no answer to this question.
Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Aug 1, 2012 at 18:05 jacktradesjacktrades 7,40214 gold badges59 silver badges84 bronze badges 5- Can you post more code? $('#foo').trigger('click'); should work... – Giovanni B Commented Aug 1, 2012 at 18:06
- Need more context on your javascript and HTML. – Mike Brant Commented Aug 1, 2012 at 18:07
- $('#foo').click() should also work – PitaJ Commented Aug 1, 2012 at 18:08
- Now, I'm assuming that your $button was defined earlier as var $button = $('button'), correct? In that case you trigger a click just like you tried with the trigger, or simply $('button').click(); – hndcrftd Commented Aug 1, 2012 at 18:09
- No it was not like this, it took a division of an existing HTML page, that performs an action outside of my script. – jacktrades Commented Aug 1, 2012 at 18:30
1 Answer
Reset to default 6Try this:
var e = document.createEvent("MouseEvents");
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
$button[0].dispatchEvent(e);
本文标签: javascriptJquery simulate a click on ltdiv rolebuttongtStack Overflow
版权声明:本文标题:javascript - Jquery simulate a click on <div role = button> - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745252022a2649879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论