Can javascript click a div?

故事扮演 提交于 2019-12-23 06:35:07

问题


Is it possible to make javascript click on a div when the div exists in the browser?

For example, the script could refresh a webpage until a div shows up (with content), and than if the div is clickable, let javascript click it (or just follow the link, if there is one).


回答1:


using jquery . use $(window).load(); function which attach all event after body load all content in web page . see below code : here you can read document of load();

working example on fiddle

 <div id="yourDivId"></div>    

  $(window).load(function () {
    $(document).on('click',"#yourDivId",function () {
         // Some code here
     });
 }); 



回答2:


Yes, it’s possible.

You can add a click event handler function using jQuery as mentioned above.

To fire the click event of that div, do

$("#mydiv").click()



回答3:


this is the correct one

<div id="mydiv></div>    
$(window).load(function () {
$(document).on('click',"#mydiv",function () {
     // Some code here
 });
}); 


来源:https://stackoverflow.com/questions/29006288/can-javascript-click-a-div

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