码迷,mamicode.com
首页 > Web开发 > 详细

js页面加载函数

时间:2016-07-16 14:11:22      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

在未加载完文档,使用jquery选择器选择元素后,如果立即绑定事件进行调用,会引起js的报错(can not read property of undefined),导致事件不能绑定成功.

alert($("p")[1].firstChild.nodeValue);

 这时就需要js的页面加载函数

一般有三种

window.onload = function(){
	$("p").bind("click", function() {
		$(this).hide();
	})
}

$(document).ready(function(){
	$("p").click(function(){
		$(this).hide();
	})
})
$(function(){
	$("p").click(function(){
		$(this).hide();
	})
})

//$(function(){})是 $(document.ready(function(){})) 的简写

 

另外jQuery绑定事件的方式有两种

一种 是直接 click(callback(){});

一种是使用 bind函数 bind("click",callback(){});

js页面加载函数

标签:

原文地址:http://www.cnblogs.com/whytohow/p/5676048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!