Javascript: 你如何使用多个 if else 语句来引用不同的元素?
Javascript: how do you use multiple if else statments refering to different elements?
我是 JavaScript 的新手,所以这可能非常明显,但我正在尝试使用红绿灯构建 Andon 系统,如果数字高于或低于某个数字,灯会改变颜色(即 > 5 = 绿色)。总共有九组灯,我已经成功地通过使用查询 select all 然后更改不透明度来根据变量更改了一组灯。
当我尝试使用第二盏灯执行此操作时,没有任何反应。我试图通过在 html 和 CSS 中以不同方式命名我的 div 元素来使其工作,例如"Zcircle"、"A1circle"
Link来码笔。
任何帮助将不胜感激!
谢谢
代码
HTML:
div class="Zcontainer">
<div class="Zcircle red" color="red">
</div>
<div class="Zcircle yellow" color="yellow"></div>
<div class="Zcircle green" color="green"></div>
</div>
<div class="A1container">
<div class="A1Circle red" color="red">
</div>
<div class="A1Circle yellow" color="yellow"></div>
<div class="A1Circle green" color="green"></div>
</div>
CSS
.Zcontainer {
background-color: #2c3e50;
border-radius: 60px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
position: relative;
left: 250px;
bottom: 75px;
padding: 15px 0;
height: 200px;
width: 70px;
}
.Zcircle {
background-color: rgba(0, 0, 0, 0.3);
border-radius: 100%;
position: relative;
height: 40px;
width: 40px;
}
.Zcircle::after {
border-right: 4px solid rgba(255, 255, 255, 0.6);
border-radius: 100%;
content: " ";
position: absolute;
top: 5px;
left: 0px;
width: 30px;
height: 30px;
}
.Zcircle.red {
background-color: #c0392b;
box-shadow: 0 0 20px 5px #c0392b;
}
.Zcircle.yellow {
background-color: #f1c40f;
box-shadow: 0 0 20px 5px #f1c40f;
}
.Zcircle.green {
background-color: #2ecc71;
box-shadow: 0 0 20px 5px #2ecc71;
}
.A1container {
background-color: #2c3e50;
border-radius: 60px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
position: relative;
left: 350px;
bottom: 275px;
padding: 15px 0;
height: 200px;
width: 70px;
}
.A1circle {
background-color: rgba(0, 0, 0, 0.3);
border-radius: 100%;
position: relative;
height: 40px;
width: 40px;
}
.A1circle::after {
border-right: 4px solid rgba(255, 255, 255, 0.6);
border-radius: 100%;
content: " ";
position: absolute;
top: 5px;
left: 0px;
width: 30px;
height: 30px;
}
.A1circle.red {
background-color: #c0392b;
box-shadow: 0 0 20px 5px #c0392b;
}
.A1circle.yellow {
background-color: #f1c40f;
box-shadow: 0 0 20px 5px #f1c40f;
}
.A1circle.green {
background-color: #2ecc71;
box-shadow: 0 0 20px 5px #2ecc71;
}
Javascript
//first traffic light - this one works
var myElements = document.querySelectorAll(".Zcircle");
for (var i = 0; i < myElements.length; i++) {
myElements[i].style.opacity = 0;
}
var a = 2 //value which will make the light change color
if (a > 4) {
var myElements = document.querySelectorAll(".Zcircle");
for (var x = 2; x < myElements.length; i++) {
myElements[x].style.opacity = 1;
}
} else if (a < 3) {
var myElements = document.querySelectorAll(".Zcircle");
for (var x = 0; x < myElements.length; i++) {
myElements[x].style.opacity = 1;
}
} else {
var myElements = document.querySelectorAll(".Zcircle");
for (var x = 1; x < myElements.length; i++) {
myElements[x].style.opacity = 1;
}
}
//second traffic light - this one doesnt work
var myElements = document.querySelectorAll(".A1circle");
for (var a = 0; a < myElements.length; a++) {
myElements[a].style.opacity = 0;
}
var b = 1; //value which will make the light change color
if (b > 4) {
var myElements = document.querySelectorAll(".A1circle");
for (var b = 2; x < myElements.length; b++) {
myElements[b].style.opacity = 1;
}
} else if (b < 3) {
var myElements = document.querySelectorAll(".A1circle");
for (var b = 0; b < myElements.length; b++) {
myElements[b].style.opacity = 1;
}
} else {
var myElements = document.querySelectorAll(".A1circle");
for (var b = 1; b < myElements.length; b++) {
myElements[b].style.opacity = 1;
}
}
这是您想要的最终代码。
let allZCircles = select(".Zcircle"),
allA1Circles = select(".A1Circle"),
items = 2;
//hides all the lights
setOpacity(allZCircles, 0);
setOpacity(allA1Circles, 0);
//as per op's requirnments
if(items >= 5) {
setOpacity(select(".Zcircle.green"), 1); // makes green light visible
setOpacity(select(".A1Circle.green"), 1); // makes green light visible
} else if (items < 3) {
setOpacity(select(".Zcircle.red"), 1); // makes red light visible
setOpacity(select(".A1Circle.red"), 1); // makes red light visible
} else {
setOpacity(select(".Zcircle.yellow"), 1); // makes yellow light visible
setOpacity(select(".A1Circle.yellow"), 1); // makes yellow light visible
}
function select(selector) {return document.querySelectorAll(selector); }
function setOpacity(selectors, opacity) {selectors.forEach((selector) => selector.style.opacity = opacity);}
我是 JavaScript 的新手,所以这可能非常明显,但我正在尝试使用红绿灯构建 Andon 系统,如果数字高于或低于某个数字,灯会改变颜色(即 > 5 = 绿色)。总共有九组灯,我已经成功地通过使用查询 select all 然后更改不透明度来根据变量更改了一组灯。
当我尝试使用第二盏灯执行此操作时,没有任何反应。我试图通过在 html 和 CSS 中以不同方式命名我的 div 元素来使其工作,例如"Zcircle"、"A1circle"
Link来码笔。
任何帮助将不胜感激! 谢谢
代码
HTML:
div class="Zcontainer">
<div class="Zcircle red" color="red">
</div>
<div class="Zcircle yellow" color="yellow"></div>
<div class="Zcircle green" color="green"></div>
</div>
<div class="A1container">
<div class="A1Circle red" color="red">
</div>
<div class="A1Circle yellow" color="yellow"></div>
<div class="A1Circle green" color="green"></div>
</div>
CSS
.Zcontainer {
background-color: #2c3e50;
border-radius: 60px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
position: relative;
left: 250px;
bottom: 75px;
padding: 15px 0;
height: 200px;
width: 70px;
}
.Zcircle {
background-color: rgba(0, 0, 0, 0.3);
border-radius: 100%;
position: relative;
height: 40px;
width: 40px;
}
.Zcircle::after {
border-right: 4px solid rgba(255, 255, 255, 0.6);
border-radius: 100%;
content: " ";
position: absolute;
top: 5px;
left: 0px;
width: 30px;
height: 30px;
}
.Zcircle.red {
background-color: #c0392b;
box-shadow: 0 0 20px 5px #c0392b;
}
.Zcircle.yellow {
background-color: #f1c40f;
box-shadow: 0 0 20px 5px #f1c40f;
}
.Zcircle.green {
background-color: #2ecc71;
box-shadow: 0 0 20px 5px #2ecc71;
}
.A1container {
background-color: #2c3e50;
border-radius: 60px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
position: relative;
left: 350px;
bottom: 275px;
padding: 15px 0;
height: 200px;
width: 70px;
}
.A1circle {
background-color: rgba(0, 0, 0, 0.3);
border-radius: 100%;
position: relative;
height: 40px;
width: 40px;
}
.A1circle::after {
border-right: 4px solid rgba(255, 255, 255, 0.6);
border-radius: 100%;
content: " ";
position: absolute;
top: 5px;
left: 0px;
width: 30px;
height: 30px;
}
.A1circle.red {
background-color: #c0392b;
box-shadow: 0 0 20px 5px #c0392b;
}
.A1circle.yellow {
background-color: #f1c40f;
box-shadow: 0 0 20px 5px #f1c40f;
}
.A1circle.green {
background-color: #2ecc71;
box-shadow: 0 0 20px 5px #2ecc71;
}
Javascript
//first traffic light - this one works
var myElements = document.querySelectorAll(".Zcircle");
for (var i = 0; i < myElements.length; i++) {
myElements[i].style.opacity = 0;
}
var a = 2 //value which will make the light change color
if (a > 4) {
var myElements = document.querySelectorAll(".Zcircle");
for (var x = 2; x < myElements.length; i++) {
myElements[x].style.opacity = 1;
}
} else if (a < 3) {
var myElements = document.querySelectorAll(".Zcircle");
for (var x = 0; x < myElements.length; i++) {
myElements[x].style.opacity = 1;
}
} else {
var myElements = document.querySelectorAll(".Zcircle");
for (var x = 1; x < myElements.length; i++) {
myElements[x].style.opacity = 1;
}
}
//second traffic light - this one doesnt work
var myElements = document.querySelectorAll(".A1circle");
for (var a = 0; a < myElements.length; a++) {
myElements[a].style.opacity = 0;
}
var b = 1; //value which will make the light change color
if (b > 4) {
var myElements = document.querySelectorAll(".A1circle");
for (var b = 2; x < myElements.length; b++) {
myElements[b].style.opacity = 1;
}
} else if (b < 3) {
var myElements = document.querySelectorAll(".A1circle");
for (var b = 0; b < myElements.length; b++) {
myElements[b].style.opacity = 1;
}
} else {
var myElements = document.querySelectorAll(".A1circle");
for (var b = 1; b < myElements.length; b++) {
myElements[b].style.opacity = 1;
}
}
这是您想要的最终代码。
let allZCircles = select(".Zcircle"),
allA1Circles = select(".A1Circle"),
items = 2;
//hides all the lights
setOpacity(allZCircles, 0);
setOpacity(allA1Circles, 0);
//as per op's requirnments
if(items >= 5) {
setOpacity(select(".Zcircle.green"), 1); // makes green light visible
setOpacity(select(".A1Circle.green"), 1); // makes green light visible
} else if (items < 3) {
setOpacity(select(".Zcircle.red"), 1); // makes red light visible
setOpacity(select(".A1Circle.red"), 1); // makes red light visible
} else {
setOpacity(select(".Zcircle.yellow"), 1); // makes yellow light visible
setOpacity(select(".A1Circle.yellow"), 1); // makes yellow light visible
}
function select(selector) {return document.querySelectorAll(selector); }
function setOpacity(selectors, opacity) {selectors.forEach((selector) => selector.style.opacity = opacity);}