如何在两组文本之间进行切换?

问题描述 投票:0回答:4

我正在尝试在两组文本之间切换开关栏。我有开关和滑块工作,但无法弄清楚如何将功能附加到滑块。

我使用W3School制作了测试环境:

https://www.w3schools.com/code/tryit.asp?filename=FVEFVIB1J6D2

切换代码格式正确,但没有采用图形格式。

function myFunction() {
    var x = document.getElementById("myDIV");
    if (x.innerHTML === "Longer text") {
        x.innerHTML = "Shorter text";
    } else {
        x.innerHTML = "Longer text";
    }
}
.switch {
  position: relative;
  display: inline-block;
  width: 90px;
  height: 34px;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #009bff;
  -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: #2ab934;
}

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

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


/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<p>
  <switch onclick="myFunction()">Choose your lenght </switch>
</p>        
<label class="switch">
  <input type="checkbox">
  <span class="slider round"></span>
</label>
<div id="myDIV">Longer Story</div>
javascript html
4个回答
0
投票

您只需要为输入更改附加一个侦听器

HTMLInputElementObject.addEventListener('input', function (evt) {
    myFunction();
});

假设“input”为HTMLInputElementObject:

function myFunction() {
    var x = document.getElementById("myDIV");
    if (x.innerHTML === "Longer text") {
        x.innerHTML = "Shorter text";
    } else {
        x.innerHTML = "Longer text";
    }
}

document.getElementById("mySlider").addEventListener('input', function (evt) {
myFunction();
});
.switch {
  position: relative;
  display: inline-block;
  width: 90px;
  height: 34px;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #009bff;
  -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: #2ab934;
}

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

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


/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;}
<label class="switch">
  <input type="checkbox" id="mySlider">
  <span class="slider round"></span>
</label>
<div id="myDIV">Longer text</div>

0
投票

你快完成了。所有你需要做的是添加一个onClick回调到span与类slider round像这样:

<label class="switch">
  <input type="checkbox">
  <span class="slider round" onclick='myFunction()'></span>
</label>

0
投票

你的onclick需要在input元素或span元素上

<label class="switch">
  <input type="checkbox" onclick="myFunction()">
  <span class="slider round"></span>
</label>

我也为你清理了你的代码,因为你的样式标签在体内,你有额外的身体标签,你的元素出于任何原因都在html标签之外。

https://www.w3schools.com/code/tryit.asp?filename=FVEGHO8VCQJA


0
投票

您必须将onclick包含在this作为参数的复选框中。

<input type="checkbox" onclick='myFunction(this)'>

this有必要检查复选框是否为checked。如果是真的,则使用第二个文本,否则使用第一个文本。

Example for multiple usage

var text = {
  1: ["Longer text", "Shorter text"],
  2: ["Another longer text", "Another shorter text"]
  // Add more
}

var output1 = document.getElementById("output");
var output2 = document.getElementById("output2");

function switchText(that, output, text) {
  switch (that.checked) {
    case true:
      output.innerHTML = text[1];
      break;
    default:
      output.innerHTML = text[0];
  }
}

function switchFirstText(that) {
  switchText(that, output1, text[1]);
}

function switchSecondText(that) {
  switchText(that, output2, text[2]);
}
.switch {
  position: relative;
  display: inline-block;
  width: 90px;
  height: 34px;
}

.switch input {
  display: none;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #009bff;
  -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: #2ab934;
}

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

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


/* Rounded sliders */

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

.slider.round:before {
  border-radius: 50%;
}
<!-- First checkbox -->

<div>Choose your length</div>

<label class="switch">
  <input onclick="switchFirstText(this)" type="checkbox">
  <span class="slider round"></span>
</label>

<div id="output">Longer text</div>

<br>
<hr>
<br>

<!-- Second checkbox -->

<div>Choose your length 2</div>

<label class="switch">
  <input onclick="switchSecondText(this)" type="checkbox">
  <span class="slider round"></span>
</label>

<div id="output2">Another longer text</div>
© www.soinside.com 2019 - 2024. All rights reserved.