ready()函数的常用语法如下。
$(document).ready(function)
由于我们只能对当前文档使用 ready()函数,因此可以跳过选择器。我们还可以如下编写上述语法。
$(function)
$(document).ready(function() {
$("p").css("color", "red");
});
$(function() {
$("p").css("color", "red");
});
<!DOCTYpe html>
<html>
<head>
<title> jQuery ready() function </title>
<script src = "/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css({"font-size": "30px", "color": "blue", "background": "yellow", "fontWeight": "bold"});
$("#p2").css({"fontSize": "20px", "fontWeight": "bold", "color": "red"});
$("#p3").css({"color": "blue"});
});
});
</script>
</head>
<body>
<p id = "p1"> Welcome to the bianchenghao6.com </p>
<p id = "p2"> This is an example of using the $(document).ready() function. </p>
<p id = "p3"> This is a third paragraph element </p>
<p> Click the following button to see the effect. </p>
<button> Click me </button>
</body>
</html>
Welcome to the bianchenghao6.com
This is an example of using the $(document).ready() function.
This is a third paragraph element
Click the following button to see the effect.