如何仅在第一次点击按钮时运行一段代码,同时让其余代码在所有点击时运行?
$button.click(function (e) {
// Only run on first click
$element.removeClass('mobile').addClass('desktop');
// Run on every click
$elementTwo.show()
});
最佳答案
只需使用一个 bool 标志变量
var st = true;
$button.click(function(e) {
// this line only execute once, since after first execution `st` will update to false
st && $element.removeClass('mobile').addClass('desktop') && st = false;
$elementTwo.show()
});
关于javascript - 在点击事件中,第一次点击时触发一次代码,其余代码在所有点击时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37120554/