我正在寻找一种方法,在点击某些内容时运行两个单独的操作/函数/"代码块",然后在再次单击相同的内容时运行完全不同的块.我把它放在一起.我想知道是否有更有效/更优雅的方式.我知道jQuery .toggle()但它有点糟糕.
在这里工作:http: //jsfiddle.net/reggi/FcvaD/1/
var count = 0;
$("#time").click(function() {
count++;
//even odd click detect
var isEven = function(someNumber) {
return (someNumber % 2 === 0) ? true : false;
};
// on odd clicks do this
if (isEven(count) === false) {
$(this).animate({
width: "260px"
}, 1500);
}
// on even clicks do this
else if (isEven(count) === true) {
$(this).animate({
width: "30px"
}, 1500);
}
});
Run Code Online (Sandbox Code Playgroud)