Home > Web Front-end > JS Tutorial > body text

What is the event of JavaScript learning? What are events used for?

青灯夜游
Release: 2018-10-16 17:47:49
forward
1878 people have browsed it

This article will introduce to you what is the event of JavaScript learning? What are events used for? , let everyone know what commonly used events are and what events can do. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First we understandWhat is an event?

Events are behaviors that can be detected by JavaScript.

HTML events are things that happen to 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.

Every element in an HTML web page can generate certain events that can trigger JavaScript functions.

The following are examples of HTML events:

  • ##HTML page completes loading

  • When the HTML input field changes

  • HTML button is clicked

Usually when an event occurs, something can be done.

When an event is triggered, JavaScript can execute some code.

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

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <span id="demo"></span>
    <button onclick=&#39;getElementById("demo").innerHTML=Date()&#39;>时间</button>
</body>
<script type="text/javascript">
    
</script>
</html>
Copy after login

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

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <span id="demo"></span>
    <button onclick=&#39;this.innerHTML=Date()&#39;>时间</button>
</body>
<script type="text/javascript">
    
</script>
</html>
Copy after login

In the above example, the JavaScript code modifies the content of its own element. (Use this.innerHTML).

JavaScript code is usually a few lines of code, and it is more common to call it through event attributes.

Common HTML events

##onchangeonclick onmouseoveronmouseoutonkeydownonload
##Events Description
## HTML element changes
The user clicks on the HTML element
The user moves the mouse over an HTML element
The user moves the mouse over an HTML element
The user presses Press the keyboard key
The browser has completed loading the page


What can JavaScript events do?

Events can be used to handle form validation, user input, user behavior and browser actions:

  • Page loading The event is triggered when

  • The event is triggered when the page is closed

  • The user clicks the button to perform the action

  • Verification Legality of user input

  • . . . . . .

You can use multiple methods to execute JavaScript event code

  • HTML event attributes can directly execute JavaScript code

  • HTML event properties can directly call JavaScript functions

  • You can specify your own event handler for HTML event properties

  • You can block events happened

  • . . . . . .

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit JavaScript Video Tutorial, jQuery Video Tutorial, bootstrap Tutorial!

The above is the detailed content of What is the event of JavaScript learning? What are events used for?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template