简体   繁体   English

Javascript:切换按钮无法正常工作

[英]Javascript: Toggle not working properly for button

I am roughly new to Vue and Javascript. 我是Vue和Javascript的新手。 I have two buttons that should change background colours when clicked. 我有两个按钮,在单击时应更改背景颜色。 I have added some Javascript code to make them toggle but the issue is that when the Free button is selected the background changes, but when the Paid button is selected it won't change. 我添加了一些Javascript代码以使它们切换,但是问题是,当选择“免费”按钮时,背景会发生变化,但是当选择“付费”按钮时,它不会发生变化。

If someone could help it would be much appreciated. 如果有人可以帮助,将不胜感激。

function changeType(type) {
  var element = document.getElementById("myDIV");
  element.classList.toggle("active");
}

Below is the Codepen for what I am trying to do. 以下是我正在尝试做的Codepen。

https://codepen.io/Aadil_Hafesji/pen/bxZWLg https://codepen.io/Aadil_Hafesji/pen/bxZWLg

Many thanks!!! 非常感谢!!!

Edit: Changed function changeType to toggle between two buttons. 编辑:更改功能changeType在两个按钮之间切换。 Also added class button to buttons. 还向按钮添加了类按钮。

You should pass event parameter to the function and toggle class of event.target. 您应该将事件参数传递给函数,并切换event.target的类。

<button class="buttonCloserGreen button" onclick="changeType(event)">
   Free
</button>

<button class="buttonCloserBlue button" onclick="changeType(event)">
   Paid
</button>

Then access target element in changeType: 然后访问changeType中的目标元素:

 function changeType(e) {
    let btns = document.getElementsByClassName('button')
    for(let i = 0; i < btns.length; i++) {
         btns[i].classList.remove("active")
    }
    e.target.classList.toggle("active");
 }

Demo 演示版

You can do like below : 您可以执行以下操作:

<button class="buttonCloserGreen" onclick="changeType(0)" id="myDIV0">
Free
</button>

<button class="buttonCloserBlue" onclick="changeType(1)" id="myDIV1">
Paid
</button>

In jquery : 在jQuery中:

 function changeType(type) {
      var element = document.getElementById("myDIV"+type);
      element.classList.toggle("active");
    }

I've passed the complete element, so you have not to use the ID/Class. 我已经通过了complete元素,因此您不必使用ID / Class。

 function changeType(element) { element.classList.toggle("active"); } 
 .buttonCloserGreen{ height: 45px; min-width: 200px; margin-bottom: 10px; border: 2px solid #53DD6C; border-radius: 8px; background-color: #ffffff; box-shadow: 4px 4px 20px 0 rgba(35, 31, 32, .05); transition: opacity 200ms ease; transition: transform 200ms cubic-bezier(.175, .885, .32, 1.275), opacity 200ms ease; font-family: Poppins; color: #53DD6C; font-size: 14px; line-height: 40px; font-weight: 700; text-align: center; letter-spacing: 0.5px; text-transform: uppercase; cursor: pointer; outline: none; } .buttonCloserBlue{ height: 45px; min-width: 200px; margin-bottom: 10px; border: 2px solid #0491F2; border-radius: 8px; background-color: #ffffff; box-shadow: 4px 4px 20px 0 rgba(35, 31, 32, .05); transition: opacity 200ms ease; transition: transform 200ms cubic-bezier(.175, .885, .32, 1.275), opacity 200ms ease; font-family: Poppins; color: #0491F2; font-size: 14px; line-height: 40px; font-weight: 700; text-align: center; letter-spacing: 0.5px; text-transform: uppercase; cursor: pointer; outline: none; } .active{ background-color: black; } 
 <button class="buttonCloserGreen" onclick="changeType(this)" id="myDIV"> Free </button> <button class="buttonCloserBlue" onclick="changeType(this)" id="myDIV"> Paid </button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM