What are the commonly used events in javascript
Commonly used events in JavaScript are: 1. Click events (onclick and ondblclick); 2. Focus events (onblur and onfocus); 3. Loading events (onload); 4. Mouse events; 5. Keyboard events ; 6. Selection and change events; 7. Form events.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Events in JS:
- Concept: After certain components perform certain operations, they trigger the execution of certain codes.
- Events: Certain operations. Such as: click, double-click, keyboard pressed, mouse moved
- Event source: component. Such as: button text input box...
- Listener: code.
- Register listening: combine events, event sources, and listeners. When an event occurs on the event source, the execution of a listener code is triggered.
1. Commonly used events
1) Click event:
- onclick: click event
- ondblclick: double-click event
2) Focus event
- onblur: lose focus
- onfocus: element gains focus.
3) Loading event:
- onload: A page or an image is loaded.
4) Mouse event:
- onmousedown The mouse button is pressed.
- onmouseup The mouse button is released.
- onmousemove The mouse is moved.
- onmouseover Move the mouse over an element.
- onmouseout The mouse moves away from an element.
5) Keyboard events:
- onkeydown A keyboard key is pressed.
- onkeyup A keyboard key is released.
- onkeypress A keyboard key is pressed and released.
6) Selection and change events
- onchange The contents of the field are changed.
- onselect The text is selected.
7) Form event:
- onsubmit The confirmation button is clicked.
- onreset The reset button is clicked.
2. Event registration
3.1. What is event registration (binding)?
In fact, it tells the browser what operation codes to execute when the event responds, which is called event registration or event binding.
3.2. Two ways to register events (static registration events, dynamic registration events)
Static registration events : The event attribute of the HTML tag is directly assigned to the code after the event response. This method is called static registration.
function sayHello() { alert("hello js!"); } <!--注册事件的第一种方式,直接在标签中使用事件句柄--> <!--以下代码的含义是:将sayHello函数注册到钮上,等待click事件发生之后, 该函数被浏览器调用。我们称这个函数为回调函数。--> <input type="button" value="hello" onclick="sayHello()"/>
Dynamic registration of events: refers to first getting the dom object of the label through js code, and then through the dom object. Event name=function(){} is assigned to the event after the event response The code is called dynamic registration.
Basic steps for dynamic registration:
1. Get the label object
2. Label object.Event name=fucntion(){}
<!--第二种注册事件的方式,是使用纯JS代码完成事件的注册。--> <input type="button" value="hello1" id="mybtn1"/> <input type="button" value="hello2" id="mybtn2"/> <input type="button" value="hello3" id="mybtn3"/> <script type="text/javascript"> function dynamic(){ alert("动态注册事件1"); } // 第一步:先获取这个钮对象() /* document:是 JavaScript语 言 提 供 的 一 个 对 象 ( 文 档 ), document是 JavaScript语 言 提 供 的 一 个 对 象 ( 文 档 ), get: 获 取 Element:元 素 ( 就 是 标 签 By:通 过 。 。 由 。 。 经 。 。 。 Id: id 属 性 getElementById: 通 过 id 属 性 获 取 标 签 对 */ var btnobj1 = document.getElementById("mybtn1"); // 第二步:给钮对象的onclick属性赋值 btnobj1.onclick = dynamic;// 注意:千万别加小括号. btnObj.onclick = doSome();这是错误的写法. // 这行代码的含义是,将回调函数doSome注册到click事件上. var btnobj2 = document.getElementById("mybtn2"); btnobj2.onclick = function(){// 这个函数没名字,叫做匿名函数,这个匿名函数也是一个回调函数. alert("动态注册事件2");// 这个函数在页面打开的时候只是注册上,不会被调用,在click事件发生之后才会调用. } document.getElementById("mybtn3").onclick = function () { alert("动态注册事件3"); } </script>
Demonstrate the Enter key 13 and the ESC key 27 through the keydown event
<body> <script type="text/javascript"> // 回车键的键值是13 // ESC键的键值是27 window.onload = function () { /* var keyvalue = document.getElementById("key"); keyvalue.onkeydown = function (event) { // 获取键值对象 alert(event);// 什么键显示都为[object KeyboardEvent] // 对于“键盘事件对象"来说,都keyCode属性用来获取键值. alert(event.keyCode);//回车键显示13 } */ var keyvalue = document.getElementById("key"); keyvalue.onkeydown = function (event) { if(event.keyCode == 13){ alert("正在进行验证") } } } </script> <input type="text" id="key"/> </body>
[Related recommendations: javascript learning tutorial】
The above is the detailed content of What are the commonly used events in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
