標籤:就會 add class html code log pre put 需要
query載入頁面的方法(頁面載入完成就執行),建議大家看下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");});
jquery載入頁面的方法(頁面載入完成就執行)