Click on element via Javascript console

醉酒当歌 提交于 2020-01-31 06:46:52

问题


Imagine there is an element #button1 on website which is watched by jQuery:

$('#button1').click(function(){
   console.log("you clicked");
};

So how do I click this #button1 element via JavaScript console? Is there a command like click("#button1")? Thanks.


回答1:


You can trigger click functio like bellow

Using JQuery

$('#button1').click()

using simple JS

document.getElementById('button1').click();



回答2:


You can trigger a click by omitting the callback function:

$('#button1').click();



回答3:


You can also use this apart from the answers mentioned already:

$( "#button1" ).trigger( "click" );



回答4:


You can not click, but simulate or trigger click. Please refer Creating and triggering events




回答5:


You can achieve this without jQuery:

document.getElementById("button1").click(); // clicks the button


来源:https://stackoverflow.com/questions/24969908/click-on-element-via-javascript-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!