35 lines
1014 B
HTML
35 lines
1014 B
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>JS事件</title>
|
|
</head>
|
|
<body>
|
|
|
|
<input type="button" id="btn1" value="点我一下试试1">
|
|
<input type="button" id="btn2" value="点我一下试试2">
|
|
|
|
<script>
|
|
//事件监听
|
|
// 1. 事件监听
|
|
// 事件源.事件类型 = 事件处理函数
|
|
// 事件源.事件类型 = function () {
|
|
// 事件处理代码
|
|
// }
|
|
|
|
// document.querySelector('#btn1').addEventListener('click', function () {
|
|
// console.log("试试就试试");
|
|
|
|
// })
|
|
//事件监听 可多次绑定同一事件
|
|
document.querySelector('#btn1').addEventListener('click', () => {
|
|
console.log("试试就试试");
|
|
})
|
|
//事件绑定早期写法 如果多次绑定 后绑定的会覆盖先绑定的
|
|
document.querySelector('#btn2').onclick = function () {
|
|
console.log("试试就试试2");
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |