如何在切换按钮中添加功能

时间:2017-11-09 17:15:20

标签: javascript jquery html css

我想在切换按钮更改时添加一些功能。目前它在切换更改方面没有任何功能。我想要一个方法,它将被调用切换更改并在它关闭时执行一些工作,如果它已打开。我想在切换更改下显示和隐藏一些链接。我附上了示例代码(CSS和HTML)。请一些人帮我写下语法。



.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  display: none;
}

.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%;
}

<h2>Toggle Switch</h2>
<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round"></span>
</label>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您可以通过在输入复选框上放置onchange函数并切换链接显示属性来使用纯JavaScript。下面的函数会在单击时查看复选框,如果选中它,则会将链接显示为块,如果未选中,则会将链接显示为无(隐藏)。

&#13;
&#13;
function toggleCheck() {
  if(document.getElementById("myCheckbox").checked === true){
    document.getElementById("aLink").style.display = "block";
  } else {
    document.getElementById("aLink").style.display = "none";
  }
}
&#13;
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  display: none;
}

.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%;
}
&#13;
<h2>Toggle Switch</h2>
<label class="switch">
  <input type="checkbox" id="myCheckbox" onchange="toggleCheck()" checked>
  <span class="slider round"></span>
</label>
<a id="aLink" href="" style="display:block;">Link</a>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  display: none;
}

.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%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h2>Toggle Switch</h2>
<label class="switch">
  <input id="toggle" type="checkbox" checked>
  <span class="slider round"></span>
</label>
<br/>
<a href="www.stackoverflow.com" id="link">Link</a>
img {
  width:50%;
  float:left;
} 
img:nth-child(3) {
    clear: both;
}

答案 2 :(得分:0)

原生JS,例如:

function handler1() {
    alert('Click!');
};
elem.addEventListener("click", handler1);
<h2>Toggle Switch</h2>
<input id="elem" type="button" value="Click me"/>
<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round" onclick="countRabbits()"></span>
</label>

或在不同情况下点击h2

function handler1() {
    alert('Click!');
};
elem.addEventListener("click", handler1);
<h2 id="elem">Toggle Switch</h2>
<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round" onclick="countRabbits()"></span>
</label>