简体   繁体   English

需要帮助调整changeImage js代码

[英]Need help adjusting changeImage js code

I'm a js beginner and I need some help to clean this code up. 我是js初学者,我需要一些帮助来清理此代码。 It's a changeImage situation, where I'm showing a check box with images rather than with an actual form check box. 这是一个changeImage情况,我在其中显示一个带有图像的复选框,而不是一个实际的表单复选框。 So the user can click an empty check box and the click changes the image to a check mark. 因此,用户可以单击一个空白复选框,然后单击会将图像更改为选中标记。 And they can click again to unchecked. 他们可以再次单击以取消选中。 That all works, but this is the issue. 一切正常,但这就是问题所在。

After you click one of the check box images, you then have to click twice to get another check box image to change. 单击一个复选框图像后,然后必须单击两次以更改另一个复选框图像。 I'd like it to be a smooth, one click situation and I don't know what's preventing that. 我希望它是一种平稳的,一键式的情况,但我不知道阻止这种情况的原因。 Here's a snippet of the code I'm using - 这是我正在使用的代码的片段-

var newsrc = "img/checked.gif";

function changeImage() {
    if(newsrc == "img/checked.gif") {
        document.images["challenge"].src = "img/checked.gif";
        document.images["challenge"].alt = "Completed";
        newsrc = "img/checkBox.gif";
    } else {
        document.images["challenge"].src = "img/checkBox.gif";
        document.images["challenge"].alt = "Unfinished";
        newsrc = "img/checked.gif";
    }
}

function changeImage2() {
    if(newsrc == "img/checked.gif") {
        document.images["goal1"].src = "img/checked.gif";
        document.images["goal1"].alt = "Completed";
        newsrc = "img/checkBox.gif";
    } else {
        document.images["goal1"].src = "img/checkBox.gif";
        document.images["goal1"].alt = "Unfinished";
        newsrc = "img/checked.gif";
    }
}

There are six images in all in the script. 脚本中总共有六个图像。 I'm just showing the first two. 我只是显示前两个。 Any help you can give would be great. 您能提供的任何帮助都会很棒。

What's happening is that all of the functions are sharing the same newsrc variable and when you click one the others think they have been clicked as well. 发生的事情是所有功能都共享同一个newsrc变量,并且当您单击一个时,其他功能也认为它们也已被单击。

You might be better off writing a single function which toggles the src and calling that function when any item is clicked. 您最好编写一个可以切换src的函数,并在单击任何项​​目时调用该函数。

Something like: 就像是:

function toggleImage(clickedImage){
    var checkedSrc='img/checked.gif';
    var currentSrc=clickedImage.src;
    if(currentSrc.indexOf(checkedSrc) ==  currentSrc.length-checkedSrc.length){
        clickedImage.src='img/checkBox.gif';
        clickedImage.alt='Unfinished';
    }else{
        clickedImage.src='img/checked.gif';
        clickedImage.alt='Complete';
    }
}

And in your html 并在您的HTML中

<img id="whatever_1" src="img/checkBox.gif" alt="Unfinished" onclick="toggleImage(this)" />
<img id="whatever_2" src="img/checkBox.gif" alt="Unfinished" onclick="toggleImage(this)" />
<img id="whatever_3" src="img/checkBox.gif" alt="Unfinished" onclick="toggleImage(this)" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM