There are three common ways to initialize a jQuery page load

  • 2020-03-30 03:11:20
  • OfStack

When the page opens, we need to perform some operations. At this time, if we choose to use jquery, we need to rewrite its 3 methods

No, it's up to you. The second feeling is simple:

The first:
 
<script type="text/javascript" src="./js/jquery-1.7.min.js"></script> 

 
<script type="text/javascript"> 
//init method one 
$(document).ready(function(){ 
trace(" The initialization method enters "); 
}); 
function trace(obj){ 
console.log(obj); 
} 
</script> 

Firefox press f12 to debug why use console.log instead of alert! This everybody should be clearer!
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201406/201406041632277.gif? 201454163242 ">  
The second:
 
<script type="text/javascript"> 
//init method one 
$(function(){ 
trace(" The initialization method enters two "); 
}); 
function trace(obj){ 
console.log(obj); 
} 
</script> 

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201406/201406041633028.gif? 201454163313 ">  
The third:
 
<script type="text/javascript" src="./js/jquery-1.7.min.js"></script> 
<script type="text/javascript"> 
//init method one 
jQuery(function($){ 
trace(" The initialization method enters three "); 
}); 
function trace(obj){ 
console.log(obj); 
} 
</script> 

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201406/201406041633379.gif? 201454163349 ">  
Ps. Instead of using jquery to initialize a page,

1. Inside the body, "onload"

2. Write it in a script
 
window.onload=function(){ 
//Something to initialize
} 

Related articles: