jQuery监听器点击不工作

jQuery listener click not working

本文关键字:工作 监听器 jQuery      更新时间:2023-09-26

所以我有这段代码,应该收听点击#按钮,但它不会工作,让我发疯!

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
    $('#button').click(function() {
        alert('OK!');
    });
</script>

和HTML:

<input id="button" type="button" value="OK" />

这很奇怪。欢迎任何帮助!

在文档中编写代码。准备好函数

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script> 
<script type="text/javascript">
$(document).ready(function() {
    $('#button').click(function() {
        alert('OK!');
    });
});
</script>

 OR
<script type="text/javascript">
    function on_click() {
          alert("OK !!");
    }
    $(document).ready(function() { 
         $("#button").click(on_click);
    });
</script>

希望你能从中得到一些启发

代码如下:

<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script> 
<script type="text/javascript"> 
    // Now that jquery is include, place the code to wire up the button here
    $(function(){
        // Once the document.onload event fires, attach the click
        // event handler for the button
        $('#button').click(function() { 
            alert('OK!'); 
        });
    });
</script> 
<input id="button" type="button" value="OK" /> 
以下是与此相关的jquery文档:http://docs.jquery.com/How_jQuery_Works