一、$(function(){
$("#a").click(function(){
//adding your code here
});
});
二、$(document).ready(function(){
$("#a").click(function(){
//adding your code here
});
});
三、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");
});html