我是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');
});
您需要在自定義文件上方有jQuery文件。你的控制檯應該有一個錯誤,說'$未定義'。 – krillgar