1

I try to use some simple native javascript code, in order to simulate a user click on a button at script.google.com:

document.querySelector("#runButton").click();

When I do it, nothing really happens, but when I really click on this element (#runButton), I can see a popup shows up.

It might have something to do with the fact that #runButton is a div, cause when I do the same with anchor() elements, it works.

Any ideas what I can do to better simulate this click? Something specific about google apps script editor that prevents me from doing it?

4

1 回答 1

3

看起来 Google Apps 脚本编辑器抑制click了界面按钮上的编程事件,这是模拟这些按钮上的“点击”的解决方案:

function doClick(n)
{
    e = document.createEvent("MouseEvents");  
    e.initEvent("mousedown", true, false);  
    n.dispatchEvent(e,true); 
    e = document.createEvent("MouseEvents");  
    e.initEvent("mouseup", true, false);  
    n.dispatchEvent(e,true);
}

// do the click
doClick(document.getElementById("runButton"));
于 2017-12-24T16:20:07.707 回答