jQuery单击“不工作”,但jQuery已加载

jQuery单击“不工作”,但jQuery已加载,jquery,click,Jquery,Click,我不明白为什么这个jQuery脚本不起作用 $( "#send" ).click(function() { console.log("ee"); }); console.log("test"); 当我加载页面时,我在控制台中看到“test”,因此jQuery页面被加载。但当我点击“发送”时,它就不起作用了 <!DOCTYPE html> <html> <head> <title>Chat-o-matic</

我不明白为什么这个jQuery脚本不起作用

$( "#send" ).click(function() {
    console.log("ee");
});

console.log("test");
当我加载页面时,我在控制台中看到“test”,因此jQuery页面被加载。但当我点击“发送”时,它就不起作用了

<!DOCTYPE html>
<html>
    <head>
        <title>Chat-o-matic</title>
        <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js" type="text/javascript"></script>
        <script src="//code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
        <script src="chat.js" type="text/javascript" type="text/javascript"></script>
        <link href="chat.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
        <div id="container">
            <h1>Chat-o-matic</h1>
            <div id="round">
                <p>Om de chat te joinen, typ een chatnaam in</p>
                <input type="text" id="chatname" placeholder="Typ hier je chatnaam in...">
                <button id="send">Join de chat</button>
            </div>
        </div>
    </body>
</html>

聊天室
聊天室
我是查特·乔宁,我是查特纳姆

加入聊天室

那么,有人知道为什么不起作用以及如何修复它吗?

将代码包装在
$(document).ready(function(){……。})中

加载dom元素后绑定事件的步骤


您的第一段javascript应该包装在document.ready函数中

$(document).ready(function(){ 
    $( "#send" ).click(function() {
        console.log("ee");
        alert("This works!");
    });
    console.log("test");
});
希望这有帮助。

试试这个

<!DOCTYPE html>
<html>
    <head>
        <title>Chat-o-matic</title>
        <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js" type="text/javascript"></script>
        <script src="//code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
        <script src="chat.js" type="text/javascript" type="text/javascript"></script>
        <link href="chat.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript">
            $(document).ready(function(){
                $( "#send" ).click(function() {
                  console.log("ee");
                });

                 console.log("test");
            });
        </script>
    </head>

    <body>
        <div id="container">
            <h1>Chat-o-matic</h1>
            <div id="round">
                <p>Om de chat te joinen, typ een chatnaam in</p>
                <input type="text" id="chatname" placeholder="Typ hier je chatnaam in...">
                <button id="send">Join de chat</button>
            </div>
        </div>
    </body>
</html>

聊天室
$(文档).ready(函数(){
$(“#发送”)。单击(函数(){
控制台日志(“ee”);
});
控制台日志(“测试”);
});
聊天室
我是查特·乔宁,我是查特纳姆

加入聊天室
允许我们将单个事件侦听器附加到父元素,该事件侦听器将为与选择器匹配的所有子体触发,无论这些子体现在存在还是将来添加

因此,将单击事件更改为以下内容

$('#container').on('click', '#send', function() {
    console.log('eee');
})

你有没有把事件绑定在一起?没有,只有一个。加入de chat查看HTML。可能重复的不要脱口而出代码。在其周围添加一些文字,解释其如何解决OP的问题。这将有助于提高答案的长期价值。
$('#container').on('click', '#send', function() {
    console.log('eee');
})