0

我有多个相同的图像。当我单击其中一个时,需要更换 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()">

4

1 回答 1

2

尝试

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";
    }
}
于 2013-06-14T13:43:05.543 回答