我试图创建一个将拨动开关恢复到关闭状态的功能。 我用开关打开或关闭灯,但是有一个按钮,如果按一下,我希望所有灯都关闭并且开关都转到“关闭” 如您所见,有4个开关和一个按钮可关闭所有设备。而且我想知道如何在HTML,CSS和JS之间进行通信
<label class="switch">
<input type="checkbox" name="one" id="one">
<span class="slider round">
</span> </label>
<label class="switch">
<input type="checkbox" name="two" id="two">
<span class="slider round">
</span> </label>
<label class="switch">
<input type="checkbox" name="three" id="three">
<span class="slider round">
</span> </label>
<label class="switch">
<input type="checkbox" name="four" id="four">
<span class="slider round">
</span> </label>
<br>
<input type="button" id="stop" value="Stop All" onclick="shutdown()"><br>
答案 0 :(得分:4)
我在这里为您创建了一个示例应用程序。 https://codesandbox.io/embed/lucid-brook-q6qsq
想法是使用document.querySelectorAll
并遍历所有复选框,然后取消选中它们。这是相关代码:
document.querySelector("#stop").addEventListener("click", () => {
const switches = document.querySelectorAll(".switch input");
for (let s of switches) {
s.checked = false;
}
});
答案 1 :(得分:3)
我建议您查找querySelectorAll
window.addEventListener("load", function() {
document.getElementById("stop").addEventListener("click", function() {
[...document.querySelectorAll(".switch input[type=checkbox]")].forEach(function(chk) {
chk.checked = false; // and perhaps add chk.onchange() if needed
});
});
});
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked+.slider {
background-color: #2196F3;
}
input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked+.slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<label class="switch">
<input type="checkbox" name="one" id="one">
<span class="slider round">
</span> </label>
<label class="switch">
<input type="checkbox" name="two" id="two">
<span class="slider round">
</span> </label>
<label class="switch">
<input type="checkbox" name="three" id="three">
<span class="slider round">
</span> </label>
<label class="switch">
<input type="checkbox" name="four" id="four">
<span class="slider round">
</span> </label>
<br>
<input type="button" id="stop" value="Stop All" /><br>
古代浏览器的替代代码
var chks = document.querySelectorAll(".switch input[type=checkbox]");
for (var i=0;i<chks.length;i++) chks[i].checked = false;
答案 2 :(得分:0)
在JavaScript中,您将使用getElementsByClassName()
方法。您必须为所有复选框添加一个类,或者可以使用getElementsByTagName()
来定位复选框标签。
let switches = document.getElementsByClassName("checkboxClass");
然后使用JavaScript遍历类
for (let i = 0; i < switches.length; i++) {
switches[i].checked = false;
}
这将全部关闭。您可以将其放在函数中,并在任何时候(onclick
/ onchange
等)调用它
希望这会有所帮助,祝您好运!