gpt4 book ai didi

javascript - 通过js更改切换按钮

转载 作者:行者123 更新时间:2023-11-28 00:41:21 24 4
gpt4 key购买 nike

我让这段代码生成一个按钮并从服务器获取一些值。

然后在加载时(初始 View )我得到值 = 1,我需要自动打开切换开关。

代码如下

var value = 1;
.switch {
position: relative;
display: inline-block;
width: 100px;
height: 34px;
}

.switch input {
display: none;
}

.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}

.slider.round {
border-radius: 34px;
}

.slider.round:before {
border-radius: 50%;
}

.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: #2ab934;
}

input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
-webkit-transform: translateX(68px);
-ms-transform: translateX(68px);
transform: translateX(68px);
}

.on {
display: none;
}

.on,
.off {
color: white;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}

input:checked+.slider .on {
display: block;
}

input:checked+.slider .off {
display: none;
}
<div align="center" id="bit00_3">
<label class="switch">
<input type="checkbox" id="bit00_3" value="off" onclick="toggle(this);">
<div class="slider round">
<span class="on">ON</span>
<span class="off">OFF</span>
</div>
</label>
</div>

所以我需要一些 javascript 在 js 中使值 1 以打开按钮。以编程方式,如果 0 关闭。

我只需要在初始化 View 中使用它

最佳答案

首先,您在同一个 html 页面中有 2 个 id,我已将输入更改为 checkbox1

其次,toggle 函数不存在且未被使用,至少在示例中是这样,所以我将其删除。

第三,我删除了您输入的值,因为您没有使用它,如果您想获取该值,只需使用 document.getElementById("checkbox1").checked 或仅使用 checkbox1.checked(将复选框分配给 var)

只需调用 checkbox1.checked = {0 for unchecked and 1 for checked} 或调用 checkbox1.checked(将复选框分配给 var)

obs:我没有用jquery,因为不知道你在这个页面上有没有用

  var value = 1;
//you can put the checkbox in a variable,
//this way you do not need to do a javascript query every time you access the value of the checkbox
var checkbox1 = document.getElementById("checkbox1")
checkbox1.checked = value
document.getElementById("checkbox1").addEventListener("change", function(element){
console.log(checkbox1.checked)
});
.switch {
position: relative;
display: inline-block;
width: 100px;
height: 34px;
}

.switch input {display:none;}

.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}

.slider.round {
border-radius: 34px;
}

.slider.round:before {
border-radius: 50%;
}

.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: #2ab934;
}

input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
-webkit-transform: translateX(68px);
-ms-transform: translateX(68px);
transform: translateX(68px);
}

.on
{
display: none;
}

.on, .off
{
color: white;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 10px;
font-family: Verdana, sans-serif;
}

input:checked+ .slider .on
{display: block;}

input:checked + .slider .off
{display: none;}
<div align="center" id="bit00_3">
<label class="switch">
<input type="checkbox" id="checkbox1">
<div class="slider round">
<span class="on">ON</span>
<span class="off">OFF</span>
</div>
</label>
</div>

关于javascript - 通过js更改切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53400766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com