I want to click a table element and to have it do x the first click and if clicked again perform Y
-
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.
- 热议问题