-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_jq_event_on.html
79 lines (63 loc) · 2.08 KB
/
01_jq_event_on.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
#box {
width: 300px;
height: 300px;
}
.red {
background-color: red;
}
.blue {
background-color: lightblue;
}
</style>
</head>
<body>
<div id="box" class="red"></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"
integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
// JQUERY 放在ready內
$(document).ready(function () {
const box = $('#box');
// 方法一
// 簡單
box.click(function () {
console.log('click ok');
});
box.mouseenter(function () {
console.log('mouseenter ok');
});
box.mouseleave(function () {
console.log('mouseleave ok');
});
// 方法二,不常用,建議還是用方法一就好
// on
// on method(object)
// object method
box.on({
mouseenter: function () {
$(this).css("background-color", "lightgray");
},
mouseleave: function () {
$(this).css("background-color", "lightblue");
},
click: function () {
$(this).css("background-color", "yellow");
}
}
, function () {
});
});
</script>
</body>
</html>