gpt4 book ai didi

javascript - 使用我的代码进行 jQuery 加载事件

转载 作者:行者123 更新时间:2023-12-02 20:19:25 24 4
gpt4 key购买 nike

好吧,我有一些 jquery javascript,我打算添加一个 .load() 事件,希望有人可以帮助我。

$(function() {

$('#nav li a img').each(function() {
var originalSrc = this.src,
hoverSrc = originalSrc.replace(/\.(gif|png|jpe?g)$/, '_over.$1');
image = new Image();

image.src = hoverSrc;

$(this).hover(function() {
this.src = hoverSrc;
}, function() {
this.src = originalSrc;
});
});
})

最佳答案

在设置 Imagesrc 属性之前,请像这样附加回调...

image.onload = function() {
// The image has loaded successfully.
}

当图像加载并成功时,它将调用该函数。

如果该函数不成功,它将调用分配给 onerror 属性的函数。

或者,您可以使用$(image).load(function() { ... })

关于javascript - 使用我的代码进行 jQuery 加载事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5800474/

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