该ready()方法用于在加载文档后使功能可用。一旦页面DOM准备执行JavaScript代码,您在$(document).ready()方法中编写的任何代码都将运行。
您可以尝试运行以下代码来学习如何在jQuery中使用$(document).ready()方法:
<html>
<head>
<title>jQuery Function</title>
<script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function() {
alert("Hello, world!");
});
});
</script>
</head>
<body>
<div id = "mydiv">
Click on this to see a dialogue box.
</div>
</body>
</html>