gpt4 book ai didi

jquery - 根据类别更改图像 src

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:14 24 4
gpt4 key购买 nike

我有三张图片,其中只有一张可以处于选中状态。他们中的任何一个都会得到一个动态添加的类。如果该类存在,我必须更改该图像的 src。这是我到目前为止的代码,jQuery 有一些问题。

<div class="estart">            
<div class="on-off-1">
<img class="" src="img/on1.png" alt="">
</div>

<div class="on-off-2">
<img class="dynamic-added-class" src="img/on2.png" alt="">
</div>

<div class="on-off-3">
<img class="" src="img/on3.png" alt="">
</div>
</div>
$("document").ready(function() {
$(".estart div img").ready(function() {
if ($('.estart div img').hasClass('dynamic-added-class')){
this.src = this.src.replace("on", "off");
}
});
});

off 图像是 off1, off2...

最佳答案

The .ready() method can only be called on a jQuery object matching the current document.

所以,不要使用 $(".estart div img").ready(function() {.

您可以使用 each如下所示:

$(document).ready(function() {//use document not 'document'
$(".estart div img").each(function() {
if ($(this).hasClass('is-selected')){
this.src = this.src.replace("on", "off");
}
});
});

关于jquery - 根据类别更改图像 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33912819/

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