jquery載入頁面的方法(頁面載入完成就執行)

來源:互聯網
上載者:User

標籤:就會   div   時間   net   read   win   nload   載入完成   query   

jquery載入頁面的方法(頁面載入完成就執行)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代碼為<input type="button" id="a">點擊</input>,且頁面需要引用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"); 
});  (轉載) http://www.jb51.net/article/27444.htm

jquery載入頁面的方法(頁面載入完成就執行)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.