gpt4 book ai didi

javascript - JQuery 函数 .load 不起作用

转载 作者:行者123 更新时间:2023-11-28 18:30:39 25 4
gpt4 key购买 nike

我有下一个代码。我想做的是当我刷新或打开页面时,添加类(class)介绍。但我的主要问题在于以下部分:

$("body").load(function(){

我想在页面打开时添加类.intro。除了body之外,我还尝试了html,但仍然不起作用。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("body").load(function(){
$("p").addClass("intro");
});
});
</script>
<style>
.intro {
background-color:green;
}
</style>
</head>
<body>

<img src="https://k32.kn3.net/taringa/1/9/3/9/4/7/32/johnny_te_toco/330x330_248.gif" width="304" height="236">

<p><b>Note:</b> Depending on the browser, the load event may not trigger if the image is cached.</p>

</body>
</html>

最佳答案

您不应该使用 load() 为此目的。

load() function description : Load data from the server and place the returned HTML into the matched element.

您只需要准备好函数即可实现这一目标:

$(document).ready(function(){
$("p").addClass("intro");
});

//OR

jQuery(function($){
$("p").addClass("intro");
});

希望这有帮助。

关于javascript - JQuery 函数 .load 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com