输入类型=按钮无法执行javascript函数

时间:2019-01-06 20:41:45

标签: javascript jquery

在此处将此问题用作参考:Using an HTML button to call a JavaScript function

jQuery ( document ).ready( function ( $ ) {

    function demoBanner () { 

        // set click handler
        $( '#demo' ).click( function ( ) {

        alert('click registered...');
        });      
    }
    // called functions
    $ ( function () {

        demoBanner();

    });
});
<input id="demo" type="button" class="btn btn-warning" value="Demo Banner">

<!--jquery CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<!-- local JS -->
<script src="http://localhost:8888/js/test.js"> </script>

为什么不调用该函数?

1 个答案:

答案 0 :(得分:-1)

尝试一下

$(document).ready(function() {
  // called functions
  demoBanner();
});
function demoBanner() {
  // set click handler
  $('#demo').click(function() {
    alert('click registered...');
  });
}