JavaScript events

JavaScript Events

HTML events are things that happen on HTML elements.

When JavaScript is used in an HTML page, JavaScript can trigger these events.

HTML events

HTML events can be browser behaviors or user behaviors.

The following are examples of HTML events:

HTML page completes loading

HTML input field changes

HTML button is clicked

Often, when an event occurs, you can do something about it.

JavaScript can execute some code when the event is triggered.

Event attributes can be added to HTML elements and JavaScript code can be used to add HTML elements.

Single quotes:

Double quotes:

in the following In the example, the onclick attribute (and code) is added to the button element:

  php中文网(php.cn) 现在的时间是?

In the above example, the JavaScript code will modify the content of the id="demo" element.

In the next example, the code will modify the content of its own element (using this.innerHTML):

  php中文网(php.cn) 现在的时间是?

JavaScript code is usually a few lines of code. The more common one is to call it through event attributes:

Common HTML events

The following is a list of some common HTML events:


##Event                                                                                                                                 The user clicks on the HTML Element

onmouseover The user moves the mouse on an HTML element

onmouseout The user removes the mouse from an HTML element


onkeydown The user presses a keyboard key

onload The browser has completed loading the page

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 the action

Verify the legality of the user input

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 handlers for HTML elements

You can prevent events from happening.

Wait...



You will learn more about events and Knowledge of event handlers.



Continuing Learning
||
<!DOCTYPE html> <html> <body> <h1 onclick="this.innerHTML='谢谢!'">请点击这里</h1> </body> </html>
submitReset Code