简体   繁体   English

选择一个按钮激活 class

[英]Selecting a button active class

these are the cards and I have written javascript to select the button and turn it into an active class but when I select the button it is not select and not turn into an active button where is the mistake这些是卡片,我已经将 javascript 写到 select 按钮并将其变成活动的 class 但是当我 select 按钮时它不是 select 并且不会变成活动按钮错误在哪里

in the style tag, I have written what all I want to do when the button is hovered or is active在样式标签中,我写了当按钮悬停或处于活动状态时我想做的所有事情

here is the javascript这是 javascript

 <script> // Add active class to the current button (highlight it) var header = document.getElementById("price"); var head = header.getElementsByClassName("card"); var btns = head.getElementsByClassName("btn"); for (var i = 0; i < btns.length; i++) { btns[i].addEventListener("click", function() { var current = document.getElementsByClassName("active"); if (current.length > 0) { current[0].className = current[0].className.replace(" active", ""); } this.className += " active"; }); } </script>
 .card.card-body a{ margin-bottom: 2.5rem; border: solid yellow; border-top-width: initial;important: color; white: background-color; transparent: font-size. 1;3rem: min-width;13rem. }.card:card-body a:hover{ color; #d6c926: {% comment %} border; none: {% endcomment %} {% comment %} border-bottom; solid blue: {% endcomment %} background-color; #191818cf: font-family;cursive: text-decoration; none: {% comment %} animation; border-line 3s ease forwards. {% endcomment %} }.card:card-body a,active. .card.card-body a:active{ background-color; #d6c926:important; color: black;important: border; black!important; font-family: cursive; }
 <div class="form-row" id="price"> <div class="card border-warning mb-3" style="max-width: 18rem;"> <div class="card-header"><strong>1 Mounth</strong></div> <div class="card-body text-warning"> <h5 class="card-title">Warning card title</h5> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> <a class="btn btn-primary active 1_mon">Select This</a> </div> </div> <div class="card border-warning mb-3" style="max-width: 18rem;"> <div class="card-header"><strong>6 Mounth</strong></div> <div class="card-body text-warning"> <h5 class="card-title">Warning card title</h5> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> <a class="btn btn-primary 6_mon">Select This</a> </div> </div> <div class="card border-warning mb-3" style="max-width: 18rem;"> <div class="card-header"><strong>10 Mounth</strong></div> <div class="card-body text-warning"> <h5 class="card-title">Warning card title</h5> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> <a class="btn btn-primary 10_mon">Select This</a> </div> </div> </div>

after this, I have to take the value of the button and store it into a hidden input tag在此之后,我必须获取按钮的值并将其存储到隐藏的输入标签中

try this:-试试这个:-

<script>
var btns = document.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function(){
  var current = document.getElementsByClassName("active");
  if (current.length > 0){ 
    current[0].className = current[0].className.replace(" active", "");
  }
  this.className += " active";
  });
}
</script>

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

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