JavaScript events
JavaScript runs in single-threaded mode in the browser. After the page is loaded, once all the JavaScript code on the page has been executed, it can only rely on trigger events to execute the JavaScript code.
After receiving the user's mouse or keyboard input, the browser will automatically trigger the corresponding event on the corresponding DOM node. If the node has been bound to the corresponding JavaScript processing function, the function will be called automatically.
JavaScript Events
The behaviors in web pages that can be detected by JavaScript are called JavaScript events. The following are some common JavaScript event examples:
1. The page content is loaded by the browser
2. The user clicks a button
3. The user presses a key
Events are usually used together with JavaScript functions, that is, event-driven functions are used to complete certain functions we want.
Common HTML events
The following is a list of some common HTML events:
EventDescription
onchange HTML element changes
onclick The user clicks on the HTML element
onmouseover The user moves the mouse on an HTML element
onmouseout The user clicks on an HTML element Move the mouse up
onkeydown The user presses the keyboard button
onload The browser has completed loading the page
onclick event
When the mouse clicks an object on the page, the onclick event will be triggered, as shown in the following example:
php中文网(php.cn) 姓名:
In the above example, the onclick event attribute is set for the OK button. The value is the "hello()" JavaScript function, that is, when the button is clicked, the hello function is executed.
php中文网(php.cn)
What can JavaScript do?
Events can be used to handle form validation, user input, user behavior and browser actions:
The event is triggered when the page is loaded. The event is triggered when the page is closed. The user clicks the button to perform actions to verify the legality of the user input content, etc...
You can use multiple methods to execute JavaScript event code:
HTML event attributes can directly execute JavaScript code
HTML event attributes can call JavaScript functions. You can specify your own event handler for
HTML elements.
You can prevent the occurrence of events.
etc ...