Click toggle with jquery/javascript

前端 未结 5 2059
粉色の甜心
粉色の甜心 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:39

    Using a state variable. The state variable will swap between the values 0 and 1 on each click. Making use of state we can execute the corresponding function in fn array.

    
    

    $("td#e1.p").each(function(){
      var state = 1, fn = [myFunction1, myFunction2];
      $(this).click(function(){
        return fn[state = 1 - state].apply(this, arguments);
      });
    });
    

    Also, it's preferably to use proper event binding than inline JavaScript.

提交回复
热议问题