gpt4 book ai didi

javascript - 用 JavaScript 替换多个图像中的一张

转载 作者:行者123 更新时间:2023-12-02 18:38:08 26 4
gpt4 key购买 nike

我有多个相同的图像。当我点击其中之一时,img 需要被替换。我有 JS 脚本:

var newsrc = "slide_down";
function changeImage() {
if ( newsrc == "slide_down" ) {
document.images["pic"].src = "img/slide_up.png";
document.images["pic"].alt = "slide_up";
newsrc = "slide_up";
}
else {
document.images["pic"].src = "img/arrow.png";
document.images["pic"].alt = "slide_down";
newsrc = "slide_down";
}
}

但是当我按下第二个img时,总是第一个被替换。请帮助。图片的html代码为<img src="img/arrow.png" alt="slide_up" class="head" id="pic" onclick="changeImage()">

最佳答案

尝试

var newsrc = "slide_down";
function changeImage() {
if ( newsrc == "slide_down" ) {
this.src = "img/slide_up.png";
this.alt = "slide_up";
newsrc = "slide_up";
}
else {
this.src = "img/arrow.png";
this.alt = "slide_down";
newsrc = "slide_down";
}
}

关于javascript - 用 JavaScript 替换多个图像中的一张,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17109767/

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