2012-03-20 154 views
1

這段代碼有什麼問題?jQuery Button沒有被點擊

<!DOCTYPE html> 
<html> 
<head> 
    <title>WebService Example</title>   
    <meta name="viewport" content="width=device-width, initial-scale=1">    
     <link rel="stylesheet" href="jquery.mobile-1.0.1.min.css" /> 
     <script src="jquery.js"></script> 
     <script src="jquery.mobile-1.0.1.min.js"></script>    
     <script type="text/javascript">     
      $("#Btn").click(function (event) { 
        alert("Button Clicked");        
       });               
     </script>    
</head> 
<body>   
    <div data-role="page"> 
     <div data-role="header" data-nobackbtn="false"> 
      <div><a href="index.html">Back</a></div> 
     </div>       
     <div data-role="content"> 
      This is the content 
     </div>       
     <div data-role="footer">      
      <a href="#" data-role="button" 
        data-icon="info" id="Btn">Button</a> 
     </div>    
    </div>   
</body> 

回答

5

的代碼運行之前DOM已準備就緒。把它放在一個DOM ready事件處理中:

$(document).ready(function() { 
    //Code that interacts with the DOM (such as binding event listeners...) 
    $("#Btn").click(function (event) { 
     alert("Button Clicked");        
    }); 
}); 
1

把腳本體內後,這樣一旦按鈕被發現瀏覽器會附加事件。

或放在document.ready中。