使用JavaScript更改活动彩色图像

时间:2019-06-13 18:05:20

标签: javascript jquery html css

请查看this JSFiddle

我已经尝试编写一些JS来制作它,以便当单击底行中的一种颜色时,顶行中的图像会发生变化。我将应更改的图像包括在JS中(它们都保存在imgur中)。

不幸的是,没有任何反应。怎么了?

HTML

<div class = "row">
  <div class="active">
  <img id="active-color" src="https://i.imgur.com/wSRf1kZ.png"/>
  </div>
</div>

<div class="row">
  <div class="other-color">
  <img id ="black" src="https://i.imgur.com/NzYkFkO.png" onclick="changeImage()"/>
  </div>

  <div class="other-color">
  <img id ="green" src="https://i.imgur.com/D2uiTnX.png" onclick="changeImage()"/>
  </div>

  <div class="other-color">
  <img id="pink" src="https://i.imgur.com/8oTZjc9.png"  onclick="changeImage()"/>
  </div>

  <div class="other-color">
  <img id="blue" src="https://i.imgur.com/oFSqOUp.png" src="https://i.imgur.com/D2uiTnX.png" onclick="changeImage()"/>
  </div>

  <div class="other-color">
  <img id="brown" src="https://i.imgur.com/xbj7l5p.png" src="https://i.imgur.com/D2uiTnX.png" onclick="changeImage()"/>
  </div>
</div>

CSS

.row {
  display: flex;
  margin-bottom: 20px;
  width: 100%;
}

.other-color {
  width: 50px;
  height: 50px;
  display: block;
}

JS

//green on white
//https://i.imgur.com/04hDBLJ.png

//pink on white
//https://i.imgur.com/FG9voBX.png

//blue on white
//https://i.imgur.com/SmNcXqY.png

//brown on white
//https://i.imgur.com/erFfzyQ.png

function changeImage() {
//if black, stay black
if (document.getElementById("active-color").src == "https://i.imgur.com/NzYkFkO.png") 
{
document.getElementById("active-color").src = "https://i.imgur.com/wSRf1kZ.png";
}
//if green, turn green
if (document.getElementById("active-color").src == "https://i.imgur.com/D2uiTnX.png") 
{
document.getElementById("active-color").src = "https://i.imgur.com/04hDBLJ.png";
}
//if pink, turn pink
if (document.getElementById("active-color").src == "https://i.imgur.com/8oTZjc9.png") 
{
document.getElementById("active-color").src = "https://i.imgur.com/FG9voBX.png";
}
//if blue, turn blue
if (document.getElementById("active-color").src == "https://i.imgur.com/D2uiTnX.png") 
{
document.getElementById("active-color").src = "https://i.imgur.com/SmNcXqY.png";
}
//if brown, turn brown
if (document.getElementById("active-color").src == "https://i.imgur.com/D2uiTnX.png") 
{
document.getElementById("active-color").src = "https://i.imgur.com/erFfzyQ.png";
}
}

也可以使用任何jQuery解决方案。

2 个答案:

答案 0 :(得分:1)

尝试一下

html:

<div class="other-color">
    <img id="black" src="https://i.imgur.com/NzYkFkO.png" onclick="changeImage(this.src)"/>
</div>

js:

function changeImage(src) {
        //if black, stay black
        if (src == "https://i.imgur.com/NzYkFkO.png"){
        document.getElementById("active-color").src = "https://i.imgur.com/wSRf1kZ.png";
    }
        //if green, turn green
        if (src == "https://i.imgur.com/D2uiTnX.png"){
        document.getElementById("active-color").src = "https://i.imgur.com/04hDBLJ.png";
    }   //if pink, turn pink
        if (src == "https://i.imgur.com/8oTZjc9.png"){
        document.getElementById("active-color").src = "https://i.imgur.com/FG9voBX.png";
    }   //if blue, turn blue
        if (src == "https://i.imgur.com/oFSqOUp.png"){
        document.getElementById("active-color").src = "https://i.imgur.com/SmNcXqY.png";
    }   //if brown, turn brown
        if (src == "https://i.imgur.com/xbj7l5p.png"){
        document.getElementById("active-color").src = "https://i.imgur.com/erFfzyQ.png";
    }
} 

答案 1 :(得分:0)

您需要将所单击内容的数据传输到功能中以便进行检查。到目前为止,无法检查Javascript中单击了哪些元素并获取其src。希望这会有所帮助。

html:

<div class="other-color">
    <img id="blue" src="https://i.imgur.com/oFSqOUp.png" onclick="changeImage(this.src)"/>
</div>

js:

function changeImage(src) {
    if (src == "https://i.imgur.com/oFSqOUp.png"){
        document.getElementById("active-color").src = "https://i.imgur.com/SmNcXqY.png";
    }
}