jquery加載頁面的方法(頁面加載完成就執(zhí)行),建議大家看下windows.onload與$(document).ready之間的區(qū)別构挤。
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全部加載完成以后執(zhí)行击敌。不幸的這種方式不僅要求頁面的DOM tree全部加載完成费封,而且要求所有的外部圖片和資源全部加載完成焕妙。更不幸的是,如果外部資源孝偎,例如圖片需要很長時間來加載访敌,那么這個js效果就會讓用戶感覺失效了。
但是用jquery的方法:
$(document).ready(function() {
// 任何需要執(zhí)行的js特效$("table tr:nth-child(even)").addClass("even");
});
就僅僅只需要加載所有的DOM結構衣盾,在瀏覽器把所有的HTML放入DOM tree之前就執(zhí)行js效果寺旺。包括在加載外部圖片和資源之前。
還有一種簡寫的方式:
$(function() {
// 任何需要執(zhí)行的js特效
$("table tr:nth-child(even)").addClass("even");
});