一个按钮在不同的点击上调用两个不同的功能 [英] one button calls two different functions on different clicks

查看:57
本文介绍了一个按钮在不同的点击上调用两个不同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script>
function one() {
   blah blah blah
}

function two() {
   blah blah blah
}
</script>

<button onclick="one(); two()">Click Me</button>

这将同时调用这两个函数。我想要的是在第一次单击时调用函数one()然后在第二次单击时调用函数two()。在第3次点击时调用函数three(),依此类推,直到第7次点击

This will call the two functions at the same time. What I want is to call function one() on the first click and then call function two() on the second click. Calls function three() on 3rd click and so on until 7th click

如果可能,我宁愿不使用jQuery

I would prefer to not use jQuery if possible

推荐答案

您可以使用 IIFE 完成此任务:

You can use an IIFE to accomplish this:

var fn3 = (function() {
  var first = true;
  return function() {
    first ? fn1() : fn2();
    first = !first;
  }
})();

function fn1() {
  console.log(1);
};
function fn2() {
  console.log(2);
};

<button onClick="fn3()">click</button>

这篇关于一个按钮在不同的点击上调用两个不同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆