【问题标题】:JavaScript if statement to change DivJavaScript if 语句改变 Div
【发布时间】:2015-10-03 13:57:58
【问题描述】:

我希望在 javascript 中获得有关我的 IF 语句的帮助。发生的事情是单击按钮时它不会改变颜色。

这就是我想要做的。单击下一个按钮时,应该会发生以下情况

如果light div的背景颜色是#ff0000(红色),它应该变成#ffff00(琥珀色)

如果light div的背景颜色是#ffff00(琥珀色),它应该变成#00ff00(绿色)

如果light div的背景颜色是#00ff00(绿色),它应该变成#ff0000(红色)

HTML:

<div class="main">
    <h1>Traffic Light</h1>
    <div class="light">
    </div>
    </br>
    <button id="next" onClick="setBgColour" type="button">Next</button>     
</div>

css:

.light 
{
    background-color: #ff0000;
    width: 100px;
    height: 20px;
    margin-left: auto;
    margin-right: auto;
}

JavaScipt:

function setBgColour(){

    if(document.getElementsByClassName("light")[0].style.backgroundColor = '#ff0000')
    {
        document.getElementsByClassName("light")[0].style.backgroundColor = '#ffff00';
    }

    else if (document.getElementsByClassName("light")[0].style.backgroundColor = '#ffff00')
    {
        document.getElementsByClassName("light")[0].style.backgroundColor = '#00ff00';
    }

    else

    document.getElementsByClassName("light")[0].style.backgroundColor = '#ff0000';

}

window.onload = function(){

    document.getElementById('next').addEventListener('click', setBgColour);

};

【问题讨论】:

  • 您应该在if 语句中使用==
  • 另外,您实际上并不需要所有这些语句 - 例如,将颜色放在数组中......
  • 根据浏览器的不同,当以您设置的相同格式读取颜色值时,您可能无法返回颜色值。将document.getElementsByClassName("light")[0].style.backgroundColor 的值记录到控制台以查看它实际包含的内容。
  • 你为什么写&lt;/br&gt;而不是&lt;br&gt;?你从哪里得到的?

标签: javascript html css if-statement


【解决方案1】:

你可以简化很多:

var lightnumber = 0; // this variable saves the current state of the traffic light
var lights = [
    "#ff0000", "#ffff00", "#00ff00"
]; // this variable saves all possible lights.

setBgColour = function() {
    // at first we decide which colour shall be next, so we increase
    // the lightnumber by one, except, if we are already at green, then we start over at 0
    lightnumber = lightnumber == (lights.length - 1) ? 0 : lightnumber + 1;
    // and here we set the light according to our lightnumber variable
    document.getElementsByClassName("light")[0].style.backgroundColor = lights[lightnumber];
}
.light {
    background-color: #ff0000;
    width: 100px;
    height: 20px;
    margin-left: auto;
    margin-right: auto;
}
<h1>Traffic Light</h1>

<div class="light"></div>
</br>
<button id="next" onClick="setBgColour()" type="button">Next</button>
</div>

【讨论】:

【解决方案2】:

你的 javascript 代码应该是这样的:-

function setBgColour() {

    if (document.getElementsByClassName("light")[0].style.backgroundColor == '#ff0000') {
        document.getElementsByClassName("light")[0].style.backgroundColor = '#ffff00';
    } else if (document.getElementsByClassName("light")[0].style.backgroundColor == '#ffff00') {
        document.getElementsByClassName("light")[0].style.backgroundColor = '#00ff00';
    } else {
        document.getElementsByClassName("light")[0].style.backgroundColor = '#ff0000';
    }

    window.onload = function () {
        document.getElementById('next').addEventListener('click', setBgColour);
    };
}

即如果你需要在里面使用==,而不是=,它是一个赋值运算符。

【讨论】:

  • 对,我看到你在那里做了什么,但是改变了我的代码。选择“下一步”按钮时,它仍然不想更改“灯光”吗?任何想法
  • 在你的 onclick html 中你写了setBgColour,而它应该是setBgColour(),因为它是一个函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
相关资源
最近更新 更多