jquery.click()没有响应

时间:2012-06-21 02:35:35

标签: jquery html

我可以知道为什么在点击按钮时出现以下内容alert('clicked')没有出现?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="Base64.js" type="text/javascript"></script>
        <script type="text/javascript">
            $("#Button1").click(function () {
                alert('clicked');
            });
        </script>
    </head>

    <body>
        <form name="input" action="">Username:
            <input type="text" id="username" value="" />
            <br />Password:
            <input type="text" id="password" value="" />
            <br />
            <input type="button" id="Button1" value="Submit" />
            <div>Login Test 1.htm</div>
        </form>
    </body>

</html>

4 个答案:

答案 0 :(得分:6)

当脚本执行时,DOM中的按钮不存在。等到DOM准备就绪jQuery.ready

$(document).ready(function() {
    $("#Button1").click(function(){alert('clicked');});
});

或者,您可以在添加按钮后的某个位置运行脚本:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="Base64.js" type="text/javascript"></script>
    </head>

    <body>
        <form name="input" action="">Username:
            <input type="text" id="username" value="" />
            <br />Password:
            <input type="text" id="password" value="" />
            <br />
            <input type="button" id="Button1" value="Submit" />
            <div>Login Test 1.htm</div>
        </form>
        <script type="text/javascript">
            $("#Button1").click(function () {
                alert('clicked');
            });
        </script>
    </body>

</html>

答案 1 :(得分:3)

试试这个,

$(document).ready(function () {
    $("#Button1").click(function(){
        alert('clicked');
    });
});

答案 2 :(得分:0)

工作演示 http://jsfiddle.net/69v27/4/

请尝试在您的代码中采购Jquery:

<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>

<强>码

$(document).ready(function() {

    $("#Button1").click(function() {
        alert('clicked');
    });

});​

答案 3 :(得分:0)

您应该使用$(function() { ... });

包围jQuery语句