2017-05-28 99 views
0

我是jquery的新手,試圖通過示例學習它。我看到有一個用於處理事件的id選擇器。這是一個非常簡單的函數來處理按鈕的點擊事件。不幸的是,它不能按預期工作。我找不到任何語法錯誤。我爲此使用jquery-3.2.1。簡單的jquery函數在單擊事件中不起作用

HTML文件

<!DOCTYPE html> 
<html> 
    <head> 
     <title>This is a test</title> 
     <script type="application/javascript" src="main.js"></script> 
     <script type="application/javascript" src="jquery-3.2.1.min.js"></script> 
    </head> 
    <body> 
     <button type="button" id="test">Click</button> 
    </body> 
</html> 

js文件:

$("#test").click(function() { 
    alert('You clicked me'); 
}); 
+0

您需要在自定義文件上方有jQuery文件。你的控制檯應該有一個錯誤,說'$未定義'。 – krillgar

回答

0

你好,我想你需要經過你的網頁是該事件將按鈕綁定完全呈現...

要做到這一點使用以下內容:

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