两次点击事件进行切换
var toggle = (function () { var a = true; return function (fn1, fn2) { a = !a; var toggler = function () { if (a) { return fn1; } else { return fn2; } }; return this.onclick = toggler(); } })();
使用方法:
var div = document.getElementsByTagName("div")[0]; div.onclick = function () { toggle(function () { div.className = "red"; }, function () { div.className = "orange"; }); };
时间: 2024-10-03 23:02:15