简体   繁体   English

使用jQuery向on body添加onload函数的正确方法

[英]Proper way to add onload function to body using jQuery

On one of my previous pages I use 在我以前的一个页面中,我使用了

 <script src="/js/wireEvent.js"></script> 
<body onload="wireEvent();" >

Basically, wireEvent() displays an image on top of the page whenever the mouse goes out of the browser. 基本上,只要鼠标离开浏览器,wireEvent()就会在页面顶部显示一个图像。 However, now I cannot add this function directly to the body of my page, so what is the proper way to add it? 但是,现在我无法将此功能直接添加到我的页面正文中,那么添加它的正确方法是什么? Will it be right if I add 如果我加上会是正确的吗?

<script>
 window.onload = wireEvent;
</script> 

or I enclose the whole function in 或者我将整个功能包括在内

 $( document ).ready 

and only include the function's file to my html? 并且只将函数的文件包含在我的html中?

The code below executes only after all your images load completely. 以下代码仅在所有图像完全加载后执行。 So you can try this: 所以你可以尝试这个:

$(document).on('load', function(){
   wireEvent();
});

You can just do: 你可以这样做:

$(function(){ 
//jQuery code here 
});

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

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