简体   繁体   中英

Click event on body after page load in jQuery?

How to simulate up an automatic real click event on body after page load in Jquery?

This is not working as it needs me to manually click the body before the automatic event registers. I need to automatically simulate a real click event on page load. Below is what I have tried:

$("document").ready(function() {
   $("body,html").click(function() {
     console.log("Automatic Click");
  });
});

You can simulate a click with:

 $( "body" ).trigger( "click" );

You can read more about it here:

https://api.jquery.com/trigger/

You can use click() like this:

$("body,html").click()

 $("document").ready(function() { $("body,html").click(function() { // catch trigger the click console.log("Automatic Click"); }); $("body,html").click(); // trigger the click });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM