You can write JavaScript code that executes when an event occurs, such as when a user clicks an HTML element, moves the mouse, or submits a form. When an event occurs on a target element, a handler function is executed. Common HTML events include:
Event handlers can be assigned to elements. For example:
事件處理器(handlers)可以被指派給元素。 例如:
var x = document.getElementById("demo"); x.onclick = function () { document.body.innerHTML = Date(); }
TIP:You can attach events to almost all HTML elements.
提示:你可以在幾乎所有的HTML元素加上事件。
-----
Events
事件
The onload and onunload events are triggered when the user enters or leaves the page. These can be useful when performing actions after the page is loaded.
Similarly, the window.onload event can be used to run code after the whole page is loaded.
window.onload = function() { //some code }
同樣的,window.onload事件,在整個網頁載入後,可以運作跑程式。
The onchange event is mostly used on textboxes. The event handler gets called when the text inside the textbox changes and focus is lost from the element. For example:
<input type="text" id="name" onchange="change()"> <script> function change() { var x = document.getElementById("name"); x.value= x.value.toUpperCase(); } </script>
TIP:It's important to understand events, because they are an essential part of dynamic web pages.
提示:了解事件很重要,因為他們是動態(dynamic)網頁必要的部分。
-----
Event Listeners
事件監聽器
The addEventListener() method attaches an event handler to an element without overwriting existing event handlers. You can add many event handlers to one element. You can also add many event handlers of the same type to one element, i.e., two "click" events.
The first parameter is the event's type (like "click" or "mousedown"). The second parameter is the function we want to call when the event occurs. The third parameter is a Boolean value specifying whether to use event bubbling or event capturing. This parameter is optional, and will be described in the next lesson.
Note that you don't use the "on" prefix for this event; use "click" instead of "onclick".Example:
Let's create an event handler that removes itself after being executed:
我們來新增一個事件監聽器,在執行後移除自己。
<button id="demo">Start</button>
<script> var btn = document.getElementById("demo"); btn.addEventListener("click", myFunction);
function myFunction() { alert(Math.random()); btn.removeEventListener("click", myFunction); } </script>
After clicking the button, an alert with a random number displays and the event listener is removed.
按下按鈕後,呈現一個亂數的警告視窗以及事件監聽器就被移除。
TIP:Internet Explorer version 8 and lower do not support the addEventListener() and removeEventListener() methods. However, you can use the document.attachEvent() method to attach event handlers in Internet Explorer.
翻譯小心得| 之前幾乎都是使用click事件,沒有特別研究滑鼠的事件,在翻譯時對click和mousedown有什麼差異覺得困惑,好像都是按下去的樣子,再查一下補充資料,理解click是按下並放開的連續動作,可以分開看成mousedown+mouseup,按下滑鼠和放開,可以有不同的事件設定。 Finally, the article above is from SOLOLEARN . I translated it just for practice, personal use only. If that is inappropriate, please contact me.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.