通过这种方式,我想了解 JavaScript Module Pattern With jQuery。 我自己有一个简单的任务,有一个按钮和三个 jQuery 函数,我想在单击按钮后,一个 function1 调用另一个 function2,而 fucnction2 调用 function3 并带有警报消息。
我的 jQuery 和 html 代码。
$(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>
感谢您的建议。
最佳答案
试试这个方法
$(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>
关于javascript - 如何调用jQuery模块函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47306120/