Events
之前設計的每一個函數,都是直接給指令執行,當操作網頁時,我們希望函數在特定的情形下才會被執行,例如在文字輸入列輸入了資料或是按了一個按鈕,Event的含意便是讓我們控制函數執行的時機。以下為部分Events: >>
- Mouse:
- onclick: mouse click
- ondblclick: mouse double-click
- onmousedown: mouse button is pressed
- onmousemove: mose pointer moves
- onmouseout: mouse pointer moves out an element
- onmouseover: mouse pointe moves over an element
- onmouseuup: mouse button is released
- onmousewheel: mouse wheel is being rotated
- Keyboard:
- onkeydown: a key is pressed
- onkeypress: a key is pressed and released
- onkeyup: a key is released
- Window & Document:
- Offline: document goes offline
- onafterprint: after the document is printed
- onbeforeonload: before the document loads
- onblur: when the window loses focus
- onfocus: when the window gets focus
- onload: when the document loads
- onpagehide: when the window is hidden
- onpageshow: when the window becomes visible
- onresize: when the window is resized
- Element:
- onchange: when an element changes
- ondrag: when an element is dragged
- ondragend: at the end of a drag operation
- ondragenter: when an element has been dragged to a valid drop target
- ondragleave: when an element is being dragged out of a drop target
- ondragover: when an element is being dragged over a drop target
- ondragstart: at the start of a drag operation
- ondrop: when dragged element is being dropped
- onformchange: when a form changes
- onforminput: when a form gets user input
- oninput: when an element gets user input
- onscroll: when an element's scrollbar is being scrolled
- onselect: when an element is selected
- onsubmit: when a form is submitted
Exercise
先設計一個按鈕,事件是onclick: >> 再加上以下的函數。 接下來每按下一次按鈕就會印出一次Hello World。
一個element可以有超過一個事件,所以現在再加上一個函數。 然後在原來的button之onclick之後加上onmouseover="mouseover()",兩者中間用空白分隔,現在將滑鼠移至按鈕上方,可以看到結果。
再試一個例子。 >>
The css code:
The functions:
allowDrop(event)這個函數主要是允許將element放在另一個element之內,預設值是無法將物品拖移到另一個物品內。
Drag this.