Home  >  Article  >  Web Front-end  >  jquery加载页面的方法(页面加载完成就执行)_html/css_WEB-ITnose

jquery加载页面的方法(页面加载完成就执行)_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:37:261515browse

jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别。

 

1、$(function(){ 
  $("#a").click(function(){ 
    //adding your code here 
  }); 
}); 
2、$(document).ready(function(){ 
  $("#a").click(function(){ 
    //adding your code here   
  }); 
}); 
3、window.onload = function(){ 
  $("#a").click(function(){ 
    //adding your code here 
  }); 

html代码为点击,且页面需要引用jquery的js文件 

一般的加载页面时调用js方法如下: 

window.onload = function() { 
$("table tr:nth-child(even)").addClass("even"); //这个是jquery代码
}; 

这段代码会在整个页面的document全部加载完成以后执行。不幸的这种方式不仅要求页面的DOM tree全部加载完成,而且要求所有的外部图片和资源全部加载完成。更不幸的是,如果外部资源,例如图片需要很长时间来加载,那么这个js效果就会让用户感觉失效了。 

但是用jquery的方法: 

$(document).ready(function() { 

// 任何需要执行的js特效 
$("table tr:nth-child(even)").addClass("even"); 
}); 

就仅仅只需要加载所有的DOM结构,在浏览器把所有的HTML放入DOM tree之前就执行js效果。包括在加载外部图片和资源之前。 

还有一种简写的方式: 

$(function() { 

// 任何需要执行的js特效
$("table tr:nth-child(even)").addClass("even"); 
});

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn