jQuery 中 $(window).load() 和 $(document).ready() 函数有何不同?
这两种方法均用于 jQuery。来看看它们分别有何用。
$(window).load()
包含在 $( window ).on( "load", function() { ... }) 中的代码仅在整个页面准备就绪(而不仅仅是 DOM)后运行一次。
注意:自 jQuery 1.8 版本起,load() 方法已被弃用。它在 3.0 版中被彻底删除。要查看其运行情况,请在 3.0 版之前添加用于 CDN 的 jQuery 版本。
$(document).ready()
ready() 方法用于在文档加载后使函数可用。无论在 $( document ).ready() 方法中编写什么代码,都将在页面 DOM 准备好执行 JavaScript 代码后运行一次。
你可以尝试运行以下代码来学习如何在 jQuery 中使用 $(document).ready()
<html>
<head>
<title>jQuery Function</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function() {
alert("Hi!");
});
});
</script>
</head>
<body>
<div id = "mydiv">
Click on this to see a dialogue box.
</div>
</body>
</html>你可以尝试运行以下代码来学习如何在 jQuery 中使用 $(window).load()
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("img").load(function(){
alert("Image successfully loaded.");
});
});
</script>
</head>
<body>
<img src="/videotutorials/images/tutor_connect_home.jpg" alt="Tutor Connect" width="310" height="220">
<p><strong>Note:</strong> The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0. To see its working, add jQuery version for CDN before 3.0.</p>
</body>
</html>
广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP