admin管理员组文章数量:1287517
I'm trying to simulate a click event in with jquery. For that purpose, I loaded jquery file with this code:
var script = document.createElement("script");
script.src = ".10.1.min.js";
document.body.appendChild(script);
Then I'm trying to simulate a click on an element with a selector, like this:
jQuery(".goog-inline-block.goog-flat-menu-button-caption").trigger("mousedown")
I also tried this click event instead of mousedown:
jQuery(".goog-inline-block.goog-flat-menu-button-caption").trigger("click")
None of them gives the effect of manually clicking to the div, namely opening a new div. I attached the ss of desired behaviour.
What can be the cause for jquery not working? Or how can I simulate the effect in any other ways?
Edit: Adding jQuery seems to be successful in the sense that I can use jQuery method to select elements and change contents of them etc.
I'm trying to simulate a click event in http://translate.google. with jquery. For that purpose, I loaded jquery file with this code:
var script = document.createElement("script");
script.src = "http://code.jquery./jquery-1.10.1.min.js";
document.body.appendChild(script);
Then I'm trying to simulate a click on an element with a selector, like this:
jQuery(".goog-inline-block.goog-flat-menu-button-caption").trigger("mousedown")
I also tried this click event instead of mousedown:
jQuery(".goog-inline-block.goog-flat-menu-button-caption").trigger("click")
None of them gives the effect of manually clicking to the div, namely opening a new div. I attached the ss of desired behaviour.
What can be the cause for jquery not working? Or how can I simulate the effect in any other ways?
Edit: Adding jQuery seems to be successful in the sense that I can use jQuery method to select elements and change contents of them etc.
Share Improve this question edited Sep 4, 2013 at 10:03 Behlül asked Sep 4, 2013 at 9:52 BehlülBehlül 3,4522 gold badges31 silver badges50 bronze badges 11- 2 I'm not sure I understand - Are you trying to trigger a click on a 3rd party website using jQuery? – Rory McCrossan Commented Sep 4, 2013 at 9:54
- 1 I think @RoryMcCrossan is right, is it an iframe you're trying to get? – Praveen Commented Sep 4, 2013 at 9:55
-
Firstly,
click
is the correct thing to trigger. Secondly, are you writing an extension? – Reinstate Monica Cellio Commented Sep 4, 2013 at 9:55 - 1 @Behlül can you add more details about " I'm trying it from chrome developer tools."? – Praveen Commented Sep 4, 2013 at 9:57
- 1 @Archer first code snippet in the question is for adding jQuery to the page. It in fact adds jQuery, you can make selections using jQuery method and you can change contents of html elements etc but event firing mechanism of jQuery does not work for some reason. – Behlül Commented Sep 4, 2013 at 10:01
1 Answer
Reset to default 11The solution was creating a MouseEvent and dispatching it to the element, like this:
var e1 = document.createEvent("MouseEvents");
e1.initMouseEvent("mousedown", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
jQuery('#gt-sl-gms')[0].dispatchEvent(e1)
It seems jQuery's event mechanism uses a different dispatching system, since it can not trigger the listener function, though I didn't confirm this info.
本文标签: javascriptCannot fire a mousedown event with jqueryStack Overflow
版权声明:本文标题:javascript - Cannot fire a mousedown event with jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741289574a2370470.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论