2014-01-26 51 views
1

我不明白爲什麼這個jQuery腳本不起作用;jQuery單擊不工作,但jquery加載

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

console.log("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> 

那麼,有人知道爲什麼這不起作用,以及如何解決它?

+0

您是否在[document-ready handler](http://api.jquery.com/ready/)中綁定了事件? – Satpal

+0

不,只有一個。 請參閱HTML。 – DazDylz

+0

[Simple jQuery click not working]可能的重複(http://stackoverflow.com/questions/8716655/simple-jquery-click-not-working) – undefined

回答

1

您的JavaScript的第一塊應包裹在的document.ready函數後內$(document).ready(function(){........});

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

代碼要結合事件。

$(document).ready(function(){ 
    $("#send").click(function() { 
     console.log("ee"); 
     alert("This works!"); 
    }); 
    console.log("test"); 
}); 

希望這會有所幫助。

0

試試這個。

<!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>