https://www.runoob.com/jsref/dom-obj-event.html
## **事件總覽:**
[事件 - Web API 接口參考 | MDN](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/touchstart_event#%E4%BA%8B%E4%BB%B6%E7%B1%BB%E5%9E%8B)
1. [`afterscriptexecute`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/afterscriptexecute_event)非標準
2. [`animationcancel`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/animationcancel_event)
3. [`animationend`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/animationend_event)
4. [`animationiteration`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/animationiteration_event)
5. [`animationstart`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/animationstart_event)
6. [`auxclick`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/auxclick_event)
7. [`beforematch`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/beforematch_event "Currently only available in English (US)")實驗性
8. [`beforescriptexecute`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/beforescriptexecute_event)非標準
9. [`beforexrselect`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/beforexrselect_event "Currently only available in English (US)")實驗性
10. [`blur`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/blur_event)
11. [`click`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/click_event)
12. [`compositionend`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/compositionend_event)
13. [`compositionstart`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/compositionstart_event)
14. [`compositionupdate`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/compositionupdate_event)
15. [`contentvisibilityautostatechange`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/contentvisibilityautostatechange_event)實驗性
16. [`contextmenu`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/contextmenu_event)
17. [`copy`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/copy_event)
18. [`cut`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/cut_event)
19. [`dblclick`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/dblclick_event)
20. [`DOMActivate`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/DOMActivate_event)已棄用
21. [`DOMMouseScroll`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/DOMMouseScroll_event)非標準已棄用
22. [`error`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/error_event)
23. [`focus`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/focus_event)
24. [`focusin`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/focusin_event "Currently only available in English (US)")
25. [`focusout`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/focusout_event)
26. [`fullscreenchange`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/fullscreenchange_event)
27. [`fullscreenerror`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/fullscreenerror_event)
28. [`gesturechange`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/gesturechange_event "Currently only available in English (US)")非標準
29. [`gestureend`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/gestureend_event "Currently only available in English (US)")非標準
30. [`gesturestart`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/gesturestart_event "Currently only available in English (US)")非標準
31. [`gotpointercapture`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/gotpointercapture_event)
32. [`keydown`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/keydown_event)
33. [`keypress`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/keypress_event)已棄用
34. [`keyup`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/keyup_event)
35. [`lostpointercapture`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/lostpointercapture_event)
36. [`mousedown`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mousedown_event)
37. [`mouseenter`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mouseenter_event)
38. [`mouseleave`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mouseleave_event)
39. [`mousemove`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mousemove_event)
40. [`mouseout`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mouseout_event)
41. [`mouseover`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mouseover_event)
42. [`mouseup`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mouseup_event)
43. [`mousewheel`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mousewheel_event)非標準已棄用
44. [`MozMousePixelScroll`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/MozMousePixelScroll_event "Currently only available in English (US)")非標準已棄用
45. [`paste`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/paste_event)
46. [`pointercancel`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointercancel_event)
47. [`pointerdown`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointerdown_event)
48. [`pointerenter`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointerenter_event)
49. [`pointerleave`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointerleave_event)
50. [`pointermove`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointermove_event)
51. [`pointerout`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointerout_event)
52. [`pointerover`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointerover_event)
53. [`pointerup`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointerup_event)
54. [`scroll`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scroll_event)
55. [`scrollend`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollend_event "Currently only available in English (US)")
56. [`securitypolicyviolation`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/securitypolicyviolation_event "Currently only available in English (US)")
57. [`touchcancel`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/touchcancel_event)
58. [`touchend`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/touchend_event)
59. [`touchmove`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/touchmove_event)
60. *`touchstart`*
61. [`transitioncancel`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/transitioncancel_event)
62. [`transitionend`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/transitionend_event)
63. [`transitionrun`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/transitionrun_event "Currently only available in English (US)")
64. [`transitionstart`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/transitionstart_event)
65. [`webkitmouseforcechanged`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/webkitmouseforcechanged_event "Currently only available in English (US)")非標準
66. [`webkitmouseforcedown`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/webkitmouseforcedown_event "Currently only available in English (US)")非標準
67. [`webkitmouseforceup`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/webkitmouseforceup_event "Currently only available in English (US)")非標準
68. [`webkitmouseforcewillbegin`(en-US)](https://developer.mozilla.org/en-US/docs/Web/API/Element/webkitmouseforcewillbegin_event "Currently only available in English (US)")非標準
69. [`wheel`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/wheel_event)
[https://www.runoob.com/jsref/dom-obj-event.html](https://www.runoob.com/jsref/dom-obj-event.html)
[https://developer.mozilla.org/zh-CN/docs/Web/API/Event](https://developer.mozilla.org/zh-CN/docs/Web/API/Event)
## 使用 HTML DOM 來分配事件
```
document.getElementById("myBtn").onclick=function(){displayDate()};
```
## addEventListener() 方法
*語法:element*.addEventListener(*event, function, useCapture*);
當用戶重置窗口大小時添加事件監聽
~~~
window.addEventListener("resize", function(){
document.getElementById("demo").innerHTML = Math.random();
});
~~~
當傳遞參數值時,使用"匿名函數"調用帶參數的函數:
~~~
element.addEventListener("click", function(){ myFunction(p1, p2); });
~~~
第三個參數? 默認值為 false, 即冒泡傳遞,當值為 true 時, 事件使用捕獲傳遞。
冒泡傳遞:自上而下觸發事件? ? 自綁定的子節點元素向父節點依次傳遞(先child,然后parent。事件的觸發順序自內向外,這就是事件冒泡。)
捕獲:自下而上觸發事件? ? ? ??自綁定的子節點元素的最高父節點依次象向綁定的子節點元素傳遞(結論:先parent,然后child。事件觸發順序變更為自外向內,這就是事件捕獲。)
## **事件冒泡(Bubble)**
就是事件向上傳導,當后代元素的事件被觸發時,其祖先元素的相同事件也會觸發
例子:點擊span,父元素也觸發了點擊事件
```
<body style="width: 1000px;height: 1000px;">
<div id="info" style="width: 100px;height:100px;background-color: aquamarine;border:5px solid darkred;">
xxx<span id="s1" style="background-color: bisque;">xxx</span>
</div>
<script type="text/javascript">
window.onload=function(){
var info=document.getElementById("info");
var s1=document.getElementById("s1");
s1.onclick=function(event){
console.log("span單擊事件響應函數")
}
info.onclick=function(event){
console.log("div單擊事件響應函數")
}
document.body.onclick=function(event){
console.log("body單擊事件響應函數")
}
}
</script>
</body>
```
結果:
```
span單擊事件響應函數
div單擊事件響應函數
body單擊事件響應函數
```
大部分事件冒泡是很有用的,如果不想事件冒泡可以通過事件對象取消事件冒泡
```
<body style="width: 1000px;height: 1000px;">
<div id="info" style="width: 100px;height:100px;background-color: aquamarine;border:5px solid darkred;">
xxx<span id="s1" style="background-color: bisque;">xxx</span>
</div>
<script type="text/javascript">
window.onload=function(){
var info=document.getElementById("info");
var s1=document.getElementById("s1");
s1.onclick=function(event){
console.log("span單擊事件響應函數")
event=event||window.event;
event.cancelBubble=true;
}
info.onclick=function(event){
console.log("div單擊事件響應函數")
}
document.body.onclick=function(event){
console.log("body單擊事件響應函數")
}
}
</script>
</body>
```
結果:
```
span單擊事件響應函數
```
**利用事件冒泡處理動態元素事件綁定的方法,專業術語叫事件委托**
jquery的on方法。$("ul").on("click","li",function(){});這樣寫有事件委托。$("ul li").on();這樣寫法則沒有
例子2:比如我們想要把鼠標放在li上就變黑
```
<ul id="ft">
<li>橘子</li>
<li>蘋果</li>
</ul>
```
我們可以這樣:
~~~
$("li").on("mouseover",function(){
$(this).css("background-color","#ff0000"").siblings().css("background-color","white");
})
~~~
但是當我們用js動態在ul上添加數據的時候我們移動到新添加的里上則沒有效果,因為在給里里添加事件的時候dom上還有有新增的li所以還需要i在綁定一次
太麻煩,所以我們可以選擇**綁定他的父節點ul的事件利用冒泡來獲取動態的節點** (事件委托)
~~~
$("ul").on("mouseover",function(e){
$(e.target).css("background-color","#ddd").siblings().css("background-color","white");
})
~~~
## 事件對象
### 常量
| 靜態變量 | 描述 | DOM |
| :-- | :-- | :-- |
| CAPTURING-PHASE | 當前事件階段為捕獲階段(1) | 1 |
| AT-TARGET | 當前事件是目標階段,在評估目標事件(1) | 2 |
| BUBBLING-PHASE | 當前的事件為冒泡階段 (3) | 3 |
### 屬性
| 屬性 | 描述 | DOM |
| :-- | :-- | :-- |
| [bubbles](https://www.runoob.com/jsref/event-bubbles.html) | 返回布爾值,指示事件是否是起泡事件類型。 | 2 |
| [cancelable](https://www.runoob.com/jsref/event-cancelable.html) | 返回布爾值,指示事件是否可擁可取消的默認動作。 | 2 |
| [currentTarget](https://www.runoob.com/jsref/event-currenttarget.html) | 返回其事件監聽器觸發該事件的元素。 | 2 |
| eventPhase | 返回事件傳播的當前階段。 | 2 |
| [target](https://www.runoob.com/jsref/event-target.html) | 返回觸發此事件的元素(事件的目標節點)。 | 2 |
| [timeStamp](https://www.runoob.com/jsref/event-timestamp.html) | 返回事件生成的日期和時間。 | 2 |
| [type](https://www.runoob.com/jsref/event-type.html) | 返回當前 Event 對象表示的事件的名稱。 | 2 |
### 方法
| 方法 | 描述 | DOM |
| :-- | :-- | :-- |
| initEvent() | 初始化新創建的 Event 對象的屬性。 | 2 |
| preventDefault() | 通知瀏覽器不要執行與事件關聯的默認動作。 | 2 |
| stopPropagation() | 不再派發事件。 | 2 |
## 目標事件對象
### 方法
| 方法 | 描述 | DOM |
| :-- | :-- | :-- |
| addEventListener() | 允許在目標事件中注冊監聽事件(IE8 = attachEvent()) | 2 |
| dispatchEvent() | 允許發送事件到監聽器上 (IE8 = fireEvent()) | 2 |
| removeEventListener() | 運行一次注冊在事件目標上的監聽事件(IE8 = detachEvent()) | 2 |
## 事件監聽對象
### 方法
| 方法 | 描述 | DOM |
| :-- | :-- | :-- |
| handleEvent() | 把任意對象注冊為事件處理程序 | 2 |
## 文檔事件對象
### 方法
| 方法 | 描述 | DOM |
| :-- | :-- | :-- |
| createEvent() | ? | 2 |
## 鼠標/鍵盤事件對象
[https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
[https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent)
### 屬性
| 屬性 | 描述 | DOM |
| :-- | :-- | :-- |
| [button](https://www.runoob.com/jsref/event-button.html) | 返回當事件被觸發時,哪個鼠標按鈕被點擊。 | 2 |
| [buttons](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/buttons)| 當鼠標事件觸發的時,如果多個鼠標按鈕被按下(如果有的話),將會返回一個或者多個代表鼠標按鈕的數字。 | — |
| [clientX](https://www.runoob.com/jsref/event-clientx.html) | 返回當事件被觸發時,鼠標指針的水平坐標(相對于瀏覽器可視窗口)。 | 2 |
| [clientY](https://www.runoob.com/jsref/event-clienty.html) | 返回當事件被觸發時,鼠標指針的垂直坐標(相對于瀏覽器可視窗口)。 | 2 |
|[x](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/x)| clientX的別名。 | 實驗性|
|[y](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/y)| clientY的別名。 | 實驗性|
| [metaKey](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/metaKey)| 當鼠標事件觸發時,如果meta鍵被按下,則返回 true;。 | — |
| [movementX](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/movementX)| 鼠標指針相對于最后mousemove事件位置的X坐標。 | — |
| [movementY](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/movementY)| 鼠標指針相對于最后mousemove事件位置的Y坐標。 | — |
| [offsetX](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/offsetX)| 鼠標指針相對于目標節點內邊位置的X坐標。相對于帶有定位的父盒子的x坐標 | 實驗性|
| [offsetY](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/offsetY)| 鼠鼠標指針相對于目標節點內邊位置的Y坐標。相對于帶有定位的父盒子的y坐標 | 實驗性|
| [pageX](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/pageX)| 鼠標指針相對于整個文檔的X坐標。IE8及以下不支持 | 實驗性|
| [pageY](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/pageY)| 鼠標指針相對于整個文檔的Y坐標。IE8及以下不支持 | 實驗性|
| [screenX](https://www.runoob.com/jsref/event-screenx.html) | 鼠標指針相對于全局(屏幕)的X坐標。 | 2 |
| [screenY](https://www.runoob.com/jsref/event-screeny.html) | 鼠標指針相對于全局(屏幕)的Y坐標。 | 2 |
| [region](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/region)| 返回被點擊事件影響的點擊區域的id,如果沒有區域被影響則返回null。 | —|
| [relatedTarget](https://www.runoob.com/jsref/event-relatedtarget.html) | 返回與事件的目標節點相關的節點。 | 2 |
| [mozPressure](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/mozPressure) | 點擊事件發生時施加在觸摸屏或者平板設備的壓力量。這個數值在0.0(最小壓力)和1.0(最大壓力)之間。 | 2 |
|[mozInputSource](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/mozInputSource) | 生成事件的類型(若干?`MOZ_SOURCE_*?`常量如下列出)。可通過該屬性獲知鼠標事件是否由真實鼠標設備觸發,亦或通過觸摸事件觸發(這可能影響處理坐標事件時的精確程度) | — |
|[webkitForce](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/webkitForce)| 返回按鍵在設備上的位置 | — |
| [charCode](https://www.runoob.com/jsref/event-key-charcode.html) |點擊時施加的壓力量。。 | —|
| [altKey](https://www.runoob.com/jsref/event-altkey.html) | 返回當事件被觸發時,Alt(OS X 中是?Option或???) 是否被按下,按下返回true。【共有】 | 2 |
| [ctrlKey](https://www.runoob.com/jsref/event-ctrlkey.html) | 返回當事件被觸發時,"CTRL" 鍵是否被按下。按下返回true【共有】 | 2 |
| [shiftKey](https://www.runoob.com/jsref/event-shiftkey.html) | 返回當事件被觸發時,"SHIFT" 鍵是否被按下。【共有】 | 2 |
| [which](https://www.runoob.com/jsref/event-key-which.html) | 返回onkeypress事件觸發的鍵的值的字符代碼,或者 onkeydown 或 onkeyup 事件的鍵的代碼。已不支持,但能使用【共有】 | 2 |
|[**`KeyboardEvent`**](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/altKey)獨有屬性|||
| [`KeyboardEvent.code`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/code) | 返回一個[`DOMString`](https://developer.mozilla.org/zh-CN/docs/Web/API/DOMString),其code值代表觸發事件的物理按鍵。 | — |
|[`KeyboardEvent.isComposing`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/isComposing) | 如果在compositionstart之后和compositionend之前觸發事件,則返回一個true的布爾值。 | — |
| [key](https://www.runoob.com/jsref/event-key-key.html) | 在按下按鍵時返回按鍵的標識符。返回一個DOMString,表示事件表示的鍵的鍵值 | 3 |
| [`KeyboardEvent.locale`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/locale) | 返回一個DOMString,它表示一個語言環境字符串,該字符串指示為鍵盤配置的語言環境。 如果瀏覽器或設備不知道鍵盤的語言環境,則可以為空字符串 | 3 |
| [keyCode](https://www.runoob.com/jsref/event-key-keycode.html) | 返回onkeypress事件觸發的鍵的值的字符代碼,或者 onkeydown 或 onkeyup 事件的鍵的代碼。過時代還能使用 | 2 |
| [`KeyboardEvent.location`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/location) | 返回一個數字,該數字表示鍵在鍵盤或其他輸入設備上的位置。 上面的“鍵盤位置”中顯示了標識位置的常量列表。。 | 2 |
| [metaKey](https://www.runoob.com/jsref/event-metakey.html) | 返回當事件被觸發時,"meta" 鍵是否被按下。 | 2 |
|[`KeyboardEvent.repeat`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/repeat) | 如果按住鍵以使其自動重復,則返回一個布爾值TRUE。 | 2 |
| [`KeyboardEvent.char`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/char) | 返回表示鍵的字符值的DOMString。 如果鍵對應于可打印字符,則此值為包含該字符的非空Unicode字符串。 如果鍵沒有可打印的表示形式,則為空字符串。【已過時】 | — |
| [`KeyboardEvent.charCode`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/charCode)| 返回一個數字,該數字表示密鑰的Unicode參考號; 此屬性僅由keypress事件使用。 對于其char屬性包含多個字符的鍵,這是該屬性中第一個字符的Unicode值。 在Firefox 26中,這將返回可打印字符的代碼。【已過時】 | — |
|[`KeyboardEvent.keyIdentifier`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/keyIdentifier)| 此屬性是非標準的,為了支持KeyboardEvent.key而已棄用。 它是DOM Level 3事件的舊版本的一部分。【已過時】 | — |
| [`KeyboardEvent.keyLocation`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/keyLocation)| 這是KeyboardEvent.location的非標準棄用別名。 它是DOM Level 3事件的舊版本的一部分。【已過時】 | — |
### 方法
| 方法 | 描述 | W3C |
| :-- | :-- | :-- |
| [`MouseEvent.getModifierState()`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/getModifierState) | 返回指定修飾鍵的當前狀態。請參照KeyboardEvent.getModifierState() 查看詳情 | — |
| MouseEvent.initMouseEvent() | 初始化鼠標事件對象的值 | 2 |
| KeyboardEvent.initKeyboardEvent() | 初始化鍵盤事件對象的值.過時但來了個使用 | 3 |
|[`KeyboardEvent.initKeyEvent()`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/initKeyEvent) |初始化KeyboardEvent對象。 這僅由Firefox實現,甚至在那里不再受支持。 相反,您應該使用KeyboardEvent()構造函數。過時但還能使用 | — |
|[KeyboardEvent.getModifierState()](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/getModifierState) |返回一個布爾值,指示創建事件時是否按下了Alt,Shift,Ctrl或Meta等修飾鍵 | — |
~~~
//方法1:構造函數實例化
event = new MouseEvent(typeArg, mouseEventInit);
//方法2:事件響應函數被觸發
element.onmousemove=function(event){}
element.click=function(event){}
...
~~~
| 屬性 | 描述 | 兼容 |
| :-- | :-- | :-- |
| [altKey](https://www.runoob.com/jsref/event-altkey.html) | 返回當事件被觸發時,"ALT" 是否被按下。 | — |
| [`MouseEvent.button`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button) | 當鼠標事件觸發的時,如果鼠標按鈕被按下(如果有的話),將會返回一個數值。 | — |
例子:獲取鼠標坐標
```
<div id="areaXY" style="width: 300px;height:200px;background-color: aquamarine;border:5px solid darkred; margin: 25px;">
300*200
</div>
<div id="info" style="width: 300px;height:40px;background-color: aquamarine;border:5px solid darkred; margin: 25px;">
</div>
<script type="text/javascript">
window.onload=function(){
var info=document.getElementById("info");
var areaXY=document.getElementById("areaXY");
areaXY.onmousemove=function(event){
//IE8及以下事件響應函數被觸發時不會傳遞event對象,而是作為window的屬性保存
if(!event){
event=window.event;
}
event=event || window.event;
var X=event.clientX;
var Y=event.clientY;
var sX=event.screenX;
var sY=event.screenY;
info.innerHTML="x:"+X+" y:"+Y;
info.innerHTML+="screenX:"+sX+" screenX:"+sY;
}
}
</script>
```
例子2:參看demo/div跟隨鼠標移動
例子3:參看拖拽01
例子4:參看鼠標滾動事件
- CSS
- 達到指定寬度加載css
- 選擇器
- CSS 函數
- @media媒體查詢
- 字體
- 圖標字體
- 文本
- 光標樣式cursor
- 盒子模型
- 溢出(overflow)
- 邊框
- 不透明度opacity
- 背景(background)與漸變xx-gradient
- 輪廓(outline)與 陰影(box-shadow)
- 過渡屬性(Transition)
- 動畫屬性(Animation)
- transform變形效果旋轉,縮放,移動,傾斜等
- 顯示、隱藏與禁用
- box-sizing與resize
- 居中對齊
- css水平居中
- css垂直居中
- 文字與相鄰的元素垂直對齊
- 布局
- 高度塌陷和外邊距重疊最終解決方案
- 解決float布局時高度塌陷的最終方案after偽類元素
- 子/父元素外邊距重疊最終解決方案before偽類元素
- 傳統布局
- position布局
- position水平居中
- position垂直居中
- position水平垂直居中
- 浮動布局
- 高度塌陷和BFC
- clear
- BFC概念及觸發條件
- 表格布局
- 盒子模型布局
- 盒子水平居中布局(如margin:0 auto)
- 盒子垂直居中布局
- 相鄰元素外邊距重疊
- 行內元素的盒子模型
- 彈性伸縮布局flex
- 舊版本(IE不支持)
- 混合過渡版(僅IE10+生效)
- flex布局(新版)
- 多列布局columns
- grid網格布局(實驗性)
- 應用與總結
- 瀑布流布局
- 流式布局(響應式布局又叫百分比布局移動端一般采用)
- 用戶不能鼠標左鍵選擇文本
- 表格
- 表單
- radio
- textarea
- select
- a連接
- ul>li有序列表與ol>li無序列表
- 偽元素
- 容器寬高100%
- 瀏覽器四大內核及前綴
- 移動端開發
- 長度單位與移動端
- css_移動端開發
- rem具體解決方案
- vw具體解決方案
- 兼容性問題
- 瀏覽器默認樣式
- css預處理器
- less
- sass
- stylus
- HTML
- 標簽元素
- head的子標簽
- 文檔元素
- 文本元素
- 嵌入元素
- 分組元素
- 表格元素
- 表單元素
- input
- 標簽元素的屬性
- 全局屬性
- aria-*
- 事件on*
- data-*
- id
- class
- hidden
- style
- title
- draggable
- dropzone(實驗性)
- dir
- autocapitalize
- contenteditable
- lang
- inputmode
- accesskey
- contextmenu(移除)
- exportparts(實驗性)
- is
- itemid
- itemprop
- itemref
- itemscope
- itemtype
- XHTML遺留xml:lang和xml:base
- part(實驗性)
- slot
- spellcheck(實驗性)
- tabindex
- translate
- HTML字符實體
- 行內元素
- iframe和父頁面相互傳值,并兼容跨域問題
- a標簽嵌套解決方案
- JS
- 獲取寬度(offsetParent、clientWidth、clientHeight、offsetWidth、offsetheight、scrollWidth、scrollHeight、offsetTop、offsetLeft、scrollTop、scrollLeft)
- demo
- 全選和反選
- 定時器:
- 哪些HTML元素可以獲得焦點?
- 事件例子
- 鼠標事件
- 注冊條款
- 獲取鼠標坐標
- div跟隨鼠標移動
- 拖拽01
- 鼠標滾動事件
- 鍵盤事件
- 檢查標簽是否含有某個類
- 輪播圖
- 數組的 交集 差集 補集 并集
- 精確計算插件
- 搖獎機
- 移動端跳轉
- 基礎
- js的數據類型
- 基本類型聲明
- 引用類型聲明及用法
- 數組
- 函數
- 對象及函數原型對象
- 繼承
- js的垃圾回收機制
- javascript擴展自定義方法
- 類型轉換
- 作用域(執行上下文)及遞歸調用
- javascript事件
- 連續調用
- 排序
- 內存溢出與內存泄漏
- 系統對象
- 內置對象
- 值屬性
- Infinity
- NaN
- undefined
- globalThis
- Function 屬性
- eval()
- isFinite()
- isNaN()
- parseFloat()
- parseInt()
- decodeURI()
- decodeURIComponent()
- encodeURI()
- encodeURIComponent()
- 基本對象(Object,Function,Boolean,Symbol)
- Object
- defineProperty()
- Function
- Boolean
- Symbol
- 數字和日期對象
- Number
- Date
- BigInt
- Math
- 控制抽象化
- AsyncFunction
- Generator
- GeneratorFunction
- Promise
- Web組裝
- WebAssembly
- 結構化數據(JSON等)
- ArrayBuffer
- Atomics
- DataView
- JSON
- SharedArrayBuffer
- 使用鍵的集合對象
- Map
- Set
- WeakMap
- WeakSet
- 反射
- Reflect
- Proxy
- 可索引的集合對象(數組在這)
- Array數組
- BigInt64Array
- BigUint64Array
- Float32Array
- Float64Array
- Int16Array
- Int32Array
- Int8Array
- Uint8ClampedArray
- Uint8Array
- Uint16Array
- Uint32Array
- 國際化
- Intl
- Intl.Collator
- 文本處理(字符串與正則)
- RegExp
- String
- 錯誤對象
- Error
- InternalError
- AggregateError 實驗性
- EvalError
- RangeError
- ReferenceError
- SyntaxError
- URIError
- TypeError
- null
- TypedArray
- escape()移除但還兼容
- unescape()移除但還生效
- uneval()非標準
- arguments
- 宿主對象(DOM與Browser)
- Browser瀏覽器對象(BOM)
- Window 對象
- History 對象
- Location 對象
- Navigator 對象
- Screen 對象
- 存儲對象(localStorage與sessionStorage)
- DOM 節點對象
- EventTarget
- Node節點對象
- Document文檔節點
- HTMLDocument(HTML對象 )
- HTML 元素接口
- Element元素節點
- Attr屬性對象(與NamedNodeMap )
- DocumentType
- DocumentFragment文檔片段節點
- CharacterData
- Comment
- Text
- CDATASection
- 事件對象Event
- on-event處理器
- CustomEvent
- MouseEvent
- DragEvent
- 手勢(TouchEvent觸摸事件)
- 其他類型事件對象...
- CSSStyleDeclaration 對象
- HTMLCollection
- console對象
- MutationObserver
- 其他重要的對象(FormData與原生Ajax)
- FormData表單對象
- ajax XMLHttpRequest
- 表達式和運算符
- 算術運算符
- 賦值運算符
- 按位操作符
- 逗號操作符
- 比較操作符
- 條件運算符
- 解構賦值
- 函數表達式
- 圓括號運算符
- 邏輯運算符
- Nullish 合并操作符
- 對象初始化
- 運算符優先級
- 可選鏈
- 管道操作符 實驗性
- 屬性訪問器
- 展開語法
- 異步函數表達式
- await
- 類表達式
- delete 操作符
- function* 表達式
- in
- instanceof
- new 運算符
- new.target
- super
- this
- typeof
- void 運算符
- yield
- yield*
- 語句和聲明
- export
- default
- 控制流
- block
- break
- continue
- empty
- if...else
- switch
- throw
- try...catch
- 聲明
- const
- let
- var 描述
- 函數和類
- async function
- class
- function
- function*
- return
- 迭代
- do...while
- for
- for await...of
- for...in
- for...of
- while
- 其他
- debugger
- label
- with 移除但生效
- import
- import.meta
- 函數
- 箭頭函數
- 默認參數值
- 方法的定義
- 剩余參數
- Arguments 對象
- getter
- setter
- 類
- 類私有域
- 類元素
- 構造方法
- extends
- static
- Errors
- 更多
- 已廢棄的特性
- JavaScript 數據結構
- 詞法文法
- 屬性的可枚舉性和所有權
- 迭代協議
- 嚴格模式
- 切換到嚴格模式
- 模板字符串
- ES6(ES2015)
- Es6函數寫法
- 類class
- 導入導出模塊
- 兼容ES5
- 變量聲明
- Symbol新數據類型
- 迭代器(自定義遍歷數組)
- 生成器
- Promise異步編程
- set(集合)
- Map
- 數組新增4個方法
- 手機端事件
- bootstrap手冊
- 代碼壓縮打包
- Webpack
- 五個核心概念
- 開始
- loader
- 插件
- webpack開發環境配置
- 打包含css文件的項目
- 打包html資源
- 打包圖片資源
- 打包其他文件
- devServer(實時自動化打包)
- 總結:開發環境配置
- webpack生產環境配置
- 提取css成單獨文件
- css兼容性處理
- 壓縮css
- js語法檢查
- js兼容性處理
- js壓縮
- html壓縮
- 總結:生產環境配置
- webpack優化環境配置
- HMR( 模塊熱替換)
- source-map
- oneOf
- 緩存
- tree shaking
- code split
- demo1
- demo2
- demo3
- lazy loading
- pwa
- 多進程打包
- externals
- dll
- webpack配置詳解
- entry
- output
- module
- resolve
- dev server
- optimization
- vite
- 技能
- 前端學習路線
- 調試
- 多個版本IE瀏覽器(調試用)
- 手機端調試
- vueJS
- Element UI(一個vuejs組件)
- 瀏覽器插件開發
- 插件推薦
- 擴展文件manifest.json
- 不可視的background(常駐)頁面
- 可視頁面browser actions與page actions及八種展示方式
- 使用chrome.xxx API
- Google Chrome擴展與Web頁面/服務器之間的交互
- Google Chrome擴展中的頁面之間的數據通信
- inject-script
- chromeAPI
- pageAction
- alarms
- chrome.tabs
- chrome.runtime
- chrome.webRequest
- chrome.window
- chrome.storage
- chrome.contextMenus
- chrome.devtools
- chrome.extension
- 分類
- homepage_url 開發者或者插件主頁
- 5種類型的JS對比及消息通信
- 其它補充
- 谷歌瀏覽器截屏
- 框架及工具
- 前端UI設計網站
- 網頁中使用Unicode字符