简体   繁体   English

为单个HTML按钮的onclick事件调用动态函数

[英]Call dynamic functions for onclick event of single HTML button

I want to call more than one function for single HTML button. 我想为单个HTML按钮调用多个函数。 But If i click a button, It calls a specific function which i have mentioned. 但是,如果我单击一个按钮,它将调用我提到的特定功能。 If i click a same button after sometime, It calls to another function which is decided by me..... 如果一段时间后单击同一按钮,它将调用由我决定的另一个功能.....

For example This is my code for on click event: 例如,这是我的on click事件代码:

<button type="button" id="outputClick" onclick="output_popup(this.id)"></button>

I need to change the function of onclick event in my js file.So please help me to apply lot of functions for single button using jQuery or Javascript. 我需要在我的js文件中更改onclick事件的功能。因此,请帮助我使用jQuery或Javascript为单个按钮应用很多功能。

Note : Functions called by onclick event is unique for that click event.Please avoid collisions. 注意:onclick事件调用的函数对于该click事件是唯一的。请避免发生冲突。

A couple of options: 有两个选择:

  1. The simplest thing to do is have the function that gets called decide which of your various functions you want it to call. 最简单的事情是让要调用的函数决定要调用的是哪个函数。 That is, you just have one function called by the event handler, which varies its behavior depending on whatever the conditions are that change. 也就是说,您只有事件处理程序调用的一个函数,该函数根据发生变化的条件来改变其行为。

    You haven't said what changes, but for instance here's an example that calls a different function on each click, looping back to the first when it reaches the end: 您没有说什么会改变,但是例如,下面的示例在每次单击时调用一个不同的函数,并在到达终点时循环回到第一个:

      var functions = [ function() { snippet.log("Function #1"); }, function() { snippet.log("Function #2"); }, function() { snippet.log("Function #3"); }, function() { snippet.log("Function #4"); } ]; var index = 0; $("#outputClick").on("click", function() { functions[index](); index = (index + 1) % functions.length; }); 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button type="button" id="outputClick">Click Me</button><!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

  2. Or of course, remove the old handler and attach a new one. 或者,当然,请删除旧的处理程序并附加一个新的处理程序。

      $("#outputClick").on("click", f1); function f1() { snippet.log("Function #1"); $("#outputClick").off("click", f1).on("click", f2); } function f2() { snippet.log("Function #2"); $("#outputClick").off("click", f2).on("click", f3); } function f3() { snippet.log("Function #3"); $("#outputClick").off("click", f3).on("click", f4); } function f4() { snippet.log("Function #4"); $("#outputClick").off("click", f4).on("click", f1); } 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button type="button" id="outputClick">Click Me</button><!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

I understood that you want to run multiple functions at the same time. 我了解到您想同时运行多个功能。 Then why not to call them all in order. 那为什么不按顺序称呼他们呢。

Now they will not necessarily execute and return in order. 现在它们不必执行并按顺序返回。 to ensure that, you have to use either callbacks or promises. 为了确保这一点,您必须使用回调或Promise。

 $("#clickthis").on("click", function() { a(); b(); c(); }); function a() { $('body').append('<p>this is function a</p>'); } function b() { $('body').append('<p>this is function b</p>'); } function c() { $('body').append('<p>this is function c</p>'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="clickthis">Call Functions</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM