什么是jQuery事件.load()、. ready()、. unload()?
jQueryload()方法
该load()方法用于将事件处理程序附加到加载事件。
示例
您可以尝试运行以下代码以了解如何使用jQueryload()方法。
注意:jQuery1.8中不推荐使用该方法。终于在jQuery3.0中将其删除。要运行以下代码,请添加小于1.8的jQuery版本,
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/1.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("img").load(function(){
alert("这是一张图片。");
});
});
</script>
</head>
<body>
<img src="/videotutorials/images/coding_ground_home.jpg" alt="Coding Ground" width="310" height="270">
<p>This image will load only in jQuery version lesser than 1.8</p>
</body>
</html>jQueryready()方法
通过ready()函数可以轻松指定发生就绪事件时发生的情况。
示例
您可以尝试运行以下代码来学习如何使用ready()方法。例如,我们在这里隐藏一个元素:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<p>This is demo text.</p>
<button>Hide</button>
</body>
</html>jQueryunload()方法
如果要在离开页面导航时触发事件,请使用unload()方法。
注意:unload()jQuery1.8中不推荐使用jQuery方法。终于在jQuery3.0中将其删除。
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(window).unload(function(){
alert("Thanks! Bye!");
});
});
</script>
</head>
<body>
<p>Event triggers when you leave the page.</p>
</body>
</html>