javascript - 如何调用jQuery模块函数?

标签 javascript jquery html

通过这种方式,我想了解 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/

相关文章:

javascript - $ ('title' ).text() 在 IE 中不起作用

javascript - 使用 jQuery 在表单中查找所有输入元素的最佳方法

html - 试图让文本重叠圆形图像以在使用 Bootstrap 调整大小时表现

javascript - 如何显示两棵具有中央根的树?

javascript - 当 onsettings 事件被触发时

Jquery 显示和隐藏而不是切换

Jquery 表单提交()回调不起作用

android - 使用高度 100vh/100% 时移动浏览器页面底部的间隙

html - ul 的水平滚动条

Javascript 排序数组错误 TypeError : undefined is not an object