简体   繁体   English

在 jQuery 中加载页面后,主体上的单击事件?

[英]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?如何在 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/ https://api.jquery.com/trigger/

You can use click() like this:您可以像这样使用click()

$("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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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