这是我一直在尝试的。
当我点击‘Switk-1’和反之亦然的时候,javascript函数myFunc()中应该使用什么来获得“Switch-2”以使灰色背景变成蓝色。我对CSS或jQuery相当陌生。我认为解决办法应该是使用其中之一或两者兼而有之。如果有人能够共享代码来实现这一点,将会有很大的帮助。提前谢谢。
function myFunc() {
var checkBox1 = document.getElementById("SW1");
var checkBox2 = document.getElementById("SW2");
if (checkBox1.checked == true){
alert("SW2:"+checkBox2.checked);
//How to Enable SW2 with blue background
} else {
alert("SW2:"+checkBox2.checked);
//How to Disable SW2 with gray background
}
}.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.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%;
}<html>
<body>
<h2>Toggle Switch</h2>
<label>Switch 1: </label>
<label class="switch">
<input type="checkbox" id="SW1" onclick="myFunc()">
<span class="slider"></span>
</label><br><br>
<label>Switch 2: </label>
<label class="switch">
<input type="checkbox" id="SW2">
<span class="slider round"></span>
</label>
</body>
</html>
发布于 2020-11-25 14:10:07
这就是你想做的吗?试着运行片段。
function switchClicked(currentSwitchState, switchToToggle) {
var switchElement = document.getElementById(switchToToggle);
switchElement.checked = currentSwitchState.checked;
}.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.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%;
}<html>
<body>
<h2>Toggle Switch</h2>
<label>Switch 1: </label>
<label class="switch">
<input id="s1" type="checkbox" onclick="switchClicked(this, 's2');">
<span class="slider"></span>
</label><br><br>
<label>Switch 2: </label>
<label class="switch">
<input id="s2" type="checkbox" onclick="switchClicked(this, 's1');">
<span class="slider round"></span>
</label>
</body>
</html>
https://stackoverflow.com/questions/65005896
复制相似问题