简体   繁体   中英

How to call the jQuery module functions?

In this way, I want to understand JavaScript Module Pattern With jQuery. I have a simple task for myself, There is one button and three jQuery functions, I want after clicking on the button, one function1 call another function2, and fucnction2 call function3 with an alert message.

My jQuery and html code.

 $(document).ready(function() { $("#myBtn").click(function() { //How to add another two functions? }); }); 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type='text/javascript' src='alertScript.js'></script> </head> <body> <button type="button" class="btn btn-default btn-lg" id="myBtn">Push</button> </body> </html> 

Thanks for any advise.

try this method

 $(document).ready(function() { $("#btnclick").click(function() { console.log('function1 called'); function1(); }); }); function function1() { alert('1 call funtion2'); console.log('function2 called'); function2(); } function function2() { alert('2 call funtion3'); console.log('function3 called'); function3(); } function function3() { console.log('1 2 3 calling'); alert('function 1 call function 2 and function2 call function 3'); } 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> </head> <body> <button type="button" class="btn btn-default btn-lg" id="btnclick">Push</button> </body> </html> 

Try this:

 $(document).ready(function() { $("#myBtn").click(function() { console.log('ok'); fn1(); }); }); function fn1(){ console.log("Fn 1 Called"); fn2(); } function fn2(){ alert("Fn 2 Called"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <html> <head> </head> <body> <button type="button" class="btn btn-default btn-lg" id="myBtn">Push</button> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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