如何调用jQuery模块函数?

时间:2017-11-15 11:23:03

标签: javascript jquery html

通过这种方式,我想了解JavaScript模块模式与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>

感谢您的任何建议。

2 个答案:

答案 0 :(得分:1)

尝试这种方法

$(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>

答案 1 :(得分:0)

试试这个:

&#13;
&#13;
$(document).ready(function() {
    $("#myBtn").click(function() {
        console.log('ok');
        fn1();
    });
});

function fn1(){
console.log("Fn 1 Called");
fn2();
}

function fn2(){
alert("Fn 2 Called");
}
&#13;
<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>
&#13;
&#13;
&#13;

相关问题