【发布时间】:2014-05-02 18:56:18
【问题描述】:
我正在尝试将 Javascript 代码写入 chrome 中的控制台,以单击页面上的链接。我使用的代码是
document.getElementById("id_name").getElementsByTagName("a").dispatchEvent(MouseEvent.ClICK)
并给我这个错误:TypeError: undefined is not a function。我这样做对吗?在 HTML 元素上模拟点击事件的正确方法是什么?我也试图在没有 jQuery 的情况下做到这一点。
【问题讨论】:
-
what's the equivalent of jquery's 'trigger' method without jquery? 的副本或您在 Google How to simulate mouse click using Javascript? 中输入问题标题时的第一个链接。
-
您的具体错误来自您试图在元素集合而不是实际元素上调用
.dispatchEvent()。
标签: javascript html google-chrome mouseclick-event