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
 |  Show 6 more ments

1 Answer 1

Reset to default 11

The 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