Click toggle with jquery/javascript

前端 未结 5 2060
粉色の甜心
粉色の甜心 2021-01-07 13:17

I want to click a table element and to have it do x the first click and if clicked again perform Y

         


        
5条回答
  •  悲&欢浪女
    2021-01-07 13:56

    You could create a new atribute on the HTML element named, for example, "clickCount", and use it inside your event handler as a counter.

    Let's say you have a button like this one:

    
    

    And you have a function like this:

    function myFunction(elt) {
       // Gets the clicks count
       var count = $(elt).data("click-count");
    
       // Adds one to the click counter
       $(elt).data("click-count", ++count);  
    
       if (count == 1)
          doSomething();
       else if (count == 2)
          doSomethingElse();
    }
    

    Every time you click the button, you'll see an alert with the number of clicks you've made.

    You can use the same method and apply it to your case.

提交回复
热议问题