问题
I am creating a google chrome extension that adds a keyboard shortcut for the link "Display images below" in Gmail using jQuery.
I have tried the following to simulate the click unsuccessfully:
$("#canvas_frame").contents().find("span:contains(Display images below)").mousedown().mouseup().click();
$("#canvas_frame").contents().find("span:contains(Display images below)").click();
$("#canvas_frame").contents().find("span:contains(Display images below)").mousedown();
$("#canvas_frame").contents().find("span:contains(Display images below)").mouseup();
$("#canvas_frame").contents().find("span:contains(Display images below)").trigger('click');
Could someone point me in the right direction?
Thank you.
回答1:
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
$("#canvas_frame span:contains(Display images below)")[0].dispatchEvent(event);
Note: don't forget that GMail is multilingual, you may need a better method of selecting the element!
来源:https://stackoverflow.com/questions/4987155/how-to-click-programmatically-on-display-images-below-in-gmail-using-jquery