如何使Toggle按钮JS继续工作?

时间:2017-11-08 20:02:04

标签: javascript jquery html css togglebutton

我正在尝试使用切换按钮来更改消息,但遗憾的是,如果您继续按下,则无效。

这是我的HTML代码:

toggled = true;
togBtn.onclick = function() {
  var text = "SELL"
  if (!toggled) {
    document.getElementById("test1").style.display = "none";
    toggled = true;
  }
  if (toggled) {
    document.getElementById("test1").innerHTML = text;
    toggled = false;
  }
};
#newsletter {
  color: #ffffff;
  background: #1d3030;
  height: 60px;
}

#newsletter h1 {
  float: left;
  padding-left: 600px;
}

#newsletter form {
  float: right;
}

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

.switch input {
  display: none;
}

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

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  -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(80px);
  -ms-transform: translateX(80px);
  transform: translateX(80px);
}

.on {
  display: none;
}

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

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

input:checked+.slider .off {
  display: none;
}

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

.slider.round:before {
  border-radius: 50%;
}
<body>
  <section id="newsletter">
    <div class="container">
      <h1 id="test1">BUY</h1>
      <form method="post" action="">
        <label class="switch">
    <input type="checkbox" id="togBtn" class="togBtn">
    <div class="slider round">
    <span class="on" name="on" id="on">SELL</span>
    <span class="off" name="off" id="off">BUY</span>
    </div>
    </label>
      </form>
    </div>
    <div class="box">
    </div>
  </section>
</body>

我添加了切换的变量,用于更改HTML中的名称。 该按钮仅适用于两次点击,之后,它再也看不到任何工作了。

4 个答案:

答案 0 :(得分:0)

变化:

 document.getElementById("test1").style.display="none";

为:

document.getElementById("test1").innerHTML="BUY";

或者,如果您不想更改标题,只需按钮,您可以删除所有的javascript,按钮仍然有效,并在按下时不断更改

答案 1 :(得分:0)

你的逻辑有点偏。如果innerHTMLtoggletrue,则需要更新false。由于这一行,该元素正在消失:document.getElementById("test1").style.display="none";。在第一次点击时它是true,因此运行代码:document.getElementById("test1").innerHTML="BUY";。在第二次点击toggle等于false时,运行document.getElementById("test1").style.display="none";并删除文本。

将您的脚本更新为此,以便在toggle分别为truefalse时更新文本值:

let toggled = true;
togBtn.onclick = function(){
  if(toggled){
   document.getElementById("test1").innerHTML="SELL";
    toggled = false;
  } else  {
    document.getElementById("test1").innerHTML="BUY";
    toggled = true;
  }
};
#newsletter {
    color: #ffffff;
    background: #1d3030;
    height: 60px;
}
#newsletter h1 {
    float: left;
    padding-left: 600px;
}
#newsletter form {
    float: right;
}
.switch {
    position: relative;
    display: inline-block;
    width: 115px;
    height: 34px;
}
.switch input {
    display: none;
}
.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #e8491d;
    -webkit-transition: .4s;
    transition: .4s;
}
.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    -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(80px);
    -ms-transform: translateX(80px);
    transform: translateX(80px);
}
.on {
    display: none;
}
.on,
.off {
    color: white;
    position: absolute;
    transform: translate(-50%, -50%);
    top: 50%;
    left: 50%;
    font-size: 10px;
}
input:checked+ .slider .on {
    display: block;
}
input:checked + .slider .off {
    display: none;
}
.slider.round {
    border-radius: 34px;
}
.slider.round:before {
    border-radius: 50%;
}
<body>
  <section id="newsletter">
    <div class="container">
      <h1 id="test1">BUY</h1>

      <form method="post" action="">
        <label class="switch">
          <input type="checkbox" id="togBtn" class="togBtn">
          <div class="slider round">
            <span class="on" name="on" id="on">SELL</span>
            <span class="off" name="off" id="off">BUY</span>
          </div>
        </label>
      </form>
    </div>
    <div class="box">
    </div>
  </section>
</body>

答案 2 :(得分:0)

无需编写这些if条件。你可以在click事件上使用三元运算符来完成它,并像这样更改文本

togBtn.onclick = function() {
var  obj = document.getElementById("test1");
    obj.innerHTML == "BUY"? (obj.innerHTML = "SELL"):(obj.innerHTML = "BUY");
 
};
#newsletter {
  color: #ffffff;
  background: #1d3030;
  height: 60px;
}

#newsletter h1 {
  float: left;
  padding-left: 600px;
}

#newsletter form {
  float: right;
}

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

.switch input {
  display: none;
}

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

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  -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(80px);
  -ms-transform: translateX(80px);
  transform: translateX(80px);
}

.on {
  display: none;
}

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

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

input:checked+.slider .off {
  display: none;
}

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

.slider.round:before {
  border-radius: 50%;
}
<body>
  <section id="newsletter">
    <div class="container">
      <h1 id="test1">BUY</h1>
      <form method="post" action="">
        <label class="switch">
    <input type="checkbox" id="togBtn" class="togBtn">
    <div class="slider round">
    <span class="on" name="on" id="on">SELL</span>
    <span class="off" name="off" id="off">BUY</span>
    </div>
    </label>
      </form>
    </div>
    <div class="box">
    </div>
  </section>
</body>

答案 3 :(得分:0)

这比制作它简单得多。不需要两个public List<RecommendationImage> translateToRecommendationImages(Recommendation recommendation, List<String> rawImages) throws ServiceException { //try and whatnot for(String image: rawImages){ recommendation.addRecommendationImage(new RecommendationImage(image)); } 元素,然后切换每个元素的span。只需使用一个display并根据当前文本的内容更新其文本。

此外,您的HTML无效。您错误地使用了span元素。

label
var btn = document.querySelector(".slider.round");
var output = document.querySelector(".onOff");
var test1 = document.getElementById("test1");
btn.addEventListener("click", function () {
  var text = (output.textContent === "Buy") ? "Sell" : "Buy";
  output.textContent = text;
  test1.textContent = text;
});
#newsletter {
      color: #ffffff;
      background: #1d3030;
      height: 60px;
}

#newsletter h1 {
      float: left;
      padding-left: 600px;
}

#newsletter form {
      float: right;
}

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

.switch input {
  display: none;
}

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

    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      -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(80px);
      -ms-transform: translateX(80px);
      transform: translateX(80px);
    }

    .onOff {
      color: white;
      position: absolute;
      transform: translate(-50%,-50%);
      top: 50%;
      left: 50%;
      font-size: 10px;
    }

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

    input:checked + .slider .off {
      display: none;
    }

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

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