doc: create Event Object section
This commit is contained in:
1 parent
51d033f803
commit
0ada4cfec8
1 file changed
+19
@@ -11,3 +11,22 @@ cover: >-
|
|||||||
https://upload.wikimedia.org/wikipedia/commons/9/99/Unofficial_JavaScript_logo_2.svg
|
https://upload.wikimedia.org/wikipedia/commons/9/99/Unofficial_JavaScript_logo_2.svg
|
||||||
abbrlink: 18008
|
abbrlink: 18008
|
||||||
---
|
---
|
||||||
|
## 事件对象
|
||||||
|
事件处理函数可以附加到对象(窗口、DOM元素等)。
|
||||||
|
```javascript
|
||||||
|
source.addEventListener("click", responseFunction);
|
||||||
|
|
||||||
|
function responseFunction(e) {
|
||||||
|
// we can access the click Event object here!
|
||||||
|
}
|
||||||
|
```
|
||||||
|
当事件发生时,将创建一个event对象并传递给事件侦听器。您可以“捕获”这个事件对象作为一个可选参数,以获取有关该事件的更多信息。
|
||||||
|
```javascript
|
||||||
|
const divs = document.querySelectorAll('div');
|
||||||
|
|
||||||
|
for (let i = 0; i < divs.length; i++) {
|
||||||
|
divs[i].onclick = function (e) {
|
||||||
|
e.target.style.backgroundColor = bgChange
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in new issue
Block a user