简体   繁体   English

检查按钮是否被多次点击

[英]Check if button has been clicked more than once

I have a set of 3 tabs and everytime i click on the same tab it replays the fade-in animation and blinks and i need it to only show the animation when i click on a new tab and not display the fade-in animation when i click on the same tab.我有一组 3 个选项卡,每次我单击同一个选项卡时,它都会重播淡入动画并闪烁,我需要它仅在单击新选项卡时显示动画,而在我单击时不显示淡入动画单击同一选项卡。

Its because it removes and re-adds the same class everytime i click at it.这是因为每次我点击它时它都会删除并重新添加相同的类。

what i currently have is:我目前拥有的是:

(function ($) {
    var tabs = $(".tabs li a");
    
    tabs.on("click", function () {
      var content = this.hash.replace("/", "");
      tabs.removeClass("active");
      $(this).addClass("active");
      $("#content").find("section").hide();
      $(content).fadeIn(200);
    });
  })

<ul class="tabs">
          <li><a class="active" href="#/one">Tab 1</a></li>
          <li><a href="#/two">Tab 2</a></li>
          <li><a href="#/three">Tab 3</a></li>
        </ul>

What i have tried:我尝试过的:

// if tab is clicked/selected then remove animation
if(!$(tabs).data("clicked")) {
        $(content).fadeIn(200);
      } else {
          $(content).fadeIn(0);
      }
      $(".active").data('clicked', true);
// if click count is higher than 1 then remove animation
var trigger = $(this),
        clickCount = trigger.data('clickCount');

        clickCount++;

        trigger.data('clickCount', clickCount);

        if(clickCount > 1) {
            $(content).fadeIn(0);
        }

Have you tried like that :你有没有试过这样:

var tabs = $(".tabs li a");

tabs.on("click", function () {

  if( !$(this).hasClass("active") ){
    var content = this.hash.replace("/", "");
    $("#content").find("section").hide();
    $(content).fadeIn(200);
    $(".tabs li").find(".active").removeClass("active");
    $(this).addClass("active");
  }


});

I'd suggest always check if active class exists before adding it.我建议在添加之前始终检查active类是否存在。 And only if it doesn't exist add this class and animation.并且仅当它不存在时才添加此类和动画。 In this case if active class exists you will not be able to add this class again and the animation not be triggered again.在这种情况下,如果存在active类,您将无法再次添加此类并且不会再次触发动画。

in JavaScript在 JavaScript 中

element is the button you need specifically. element 是您特别需要的按钮。 you can get the button id using a simple Js query您可以使用简单的 Js 查询获取按钮 ID

 var element = document.getElementById('buttonID');

 element.on('click', (){ let counter = 0; return function inner() { counter++; console.log('Number of clicks: ' + counter); }; })

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

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