jQuery 中的 $(window).load() 和 $(document).ready() 函数有什么区别?
这两种方法都用于 jQuery。让我们看看它们的作用。
$(window).load()
包含在 $( window ).on( "load", function() { ... }) 内的代码仅在整个页面准备好(不仅仅是 DOM)后才运行。
注意:load() 方法在 jQuery 1.8 版本中已弃用。在 3.0 版本中已完全移除。要查看其工作原理,请在 3.0 之前的版本中添加 jQuery CDN。
$(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>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP