Home Web Front-end JS Tutorial Javascript Basics (2) Events_Basic Knowledge

Javascript Basics (2) Events_Basic Knowledge

May 16, 2016 pm 04:34 PM
javascript event basic knowledge

Event object: (The event object is a property of the window object. When an event occurs, an event object is generated at the same time. When the event ends, the event object disappears)

In IE: window.event;//Get the object

In DOM: argument[0];//Get the object

Commonly used attribute methods of Event objects in IE:

1.clientX: When the event occurs, the X coordinate of the mouse pointer in the client area (excluding toolbars, scroll bars, etc.);

2.clientY: When the event occurs, the Y coordinate of the mouse pointer in the client area (excluding toolbars, scroll bars, etc.);

3.keyCode: For keyCode events, indicate the Unicode character of the pressed key. For keydown/keyup events, indicate that the pressed keyboard is a numeric indicator (get the value of the key);

4.offsetX: X coordinate of the mouse pointer relative to the object that triggered the event;

5.offsetY: Y coordinate of the mouse pointer relative to the object that triggered the event;

6.srcElement: The element that caused the event to occur;

Commonly used attribute methods of event objects in DOM:

1.clientX;

2.clientY;

3.pageX; X coordinate of the mouse pointer relative to the page;

4.pageY; Y coordinate of the mouse pointer relative to the page;

5.StopPropagation: Calling this method can prevent the further propagation (bubble) of the event;

6.target: triggered event element/object;

7.type: name of the event;

The similarities and differences between the two event objects:

Same points:

1. Get the event type;

2. Get the keyboard code (keydown/keyup event);

3. Detect Shift, Alt, Ctrl;

4. Get the client area coordinates;

5. Get screen coordinates;

Differences:

1. Get the target;

//IE: var oTarget=oEvent.srcElement;

//DOM: var oTarget=oEvent.target;

2. Get character code;

//IE: var iCharCode=oEvent.keyCode;

//DOM: var iCharCode=oEvent.charCode;

3. Block the default behavior of events;

//IE: oEvent.returnValue=false;

//DOM: oEvent.preventDefault;

4. Terminate bubbling:

//IE:oEvent.cancelBubble=true;

//DOM:oEvent.stopPropagation

Event type:

1. Mouse events:

onmouseover: when the mouse moves in;

onmouseout: when the mouse moves out;

onmousedown: when the mouse is pressed;

onmouseup: when the mouse is lifted;

onclick: when clicking the left mouse button;

ondblclick: when double-clicking the left mouse button;

2. Keyboard events:

onkeydown: Occurs when the user presses a key on the keyboard;

onkeyup: Occurs when the user releases a pressed key;

keypress: When the user keeps pressing the key;

3. HTML events:

onload: when loading the page;

onunload: when unloading the page;

abort: When the user terminates the loading process, if it has not been completely reloaded, an abort event occurs

error: event generated when a JavaScript error occurs;

select: In an input or textarea, when the user selects a character, the select event is triggered

change: In an input or textarea, when it loses focus, the change event is triggered in the select

submit: When the form is submitted, the submit event is triggered;

reset:Reset

resize: event triggered when the window or frame size is resized;

scroll: event triggered when the user scrolls or has a scroll bar;

focus: when focus is lost;

blur: when getting focus;

Javascript event model

1.Javascript event model: 1. Bubbling type: When the user clicks the button: input-body-html-document-window (bubbles from bottom to top) IE browsing The device just uses bubbling

2. Capture type: When the user clicks the button: window-document-html-body-input (from top to bottom)

After ECMA standardization, other browsers support both types, capture happens first.

2. Three ways of writing traditional events:

1.

2.======<script>function name1(){alert('helloword!');}</script> / /Famous function

3. //Anonymous function

Copy code The code is as follows:

<script><br> Var button1=document.getElementById("input1");<br> button1.onclick=funtion(){<br> alert('helloword!')<br> }<br> </script>

3. How to write modern events:

Copy code The code is as follows:

//Add event in IE

<script><br> var fnclick(){<br> alert("I was clicked")<br> }<br> var Oinput=document.getElementById("input1");<br> Oinput.attachEvent("onclick",fnclick);<br> -----------------------------------------------<br> Oinput.detachEvent("onclick",fnclick);//Delete events in IE<br> </script>

Copy code The code is as follows:

//Add event in DOM

<script><br> var fnclick(){<br> alert("I was clicked")<br> }<br> var Oinput=document.getElementById("input1");<br> Oinput.addEventListener("onclick",fnclick,true);<br> -----------------------------------------------<br> Oinput.removeEventListener("onclick",fnclick);//Delete events in DOM<br> </script>

Copy code The code is as follows:

//Compatible with IE and DOM adding events

<script><br> var fnclick1=function(){alert("I was clicked")}<br> var fnclick2=function(){alert("I was clicked")}<br> var Oinput=document.getElementById("input1");<br> if(document.attachEvent){<br> Oinput.attachEvent("onclick",fnclick1)<br> Oinput.attachEvent("onclick",fnclick2)<br> }<br> else(document.addEventListener){<br> Oinput.addEventListener("click",fnclick1,true)<br> Oinput.addEventListener("click",fnclick2,true)<br> }<br> </script>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

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:

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How to build event-based applications using PHP How to build event-based applications using PHP May 04, 2024 pm 02:24 PM

Methods for building event-based applications in PHP include using the EventSourceAPI to create an event source and using the EventSource object to listen for events on the client side. Send events using Server Sent Events (SSE) and listen for events on the client side using an XMLHttpRequest object. A practical example is to use EventSource to update inventory counts in real time in an e-commerce website. This is achieved on the server side by randomly changing the inventory and sending updates, and the client listens for inventory updates through EventSource and displays them in real time.

A Deep Dive into Close Button Events in jQuery A Deep Dive into Close Button Events in jQuery Feb 24, 2024 pm 05:09 PM

In-depth understanding of the close button event in jQuery During the front-end development process, we often encounter situations where we need to implement the close button function, such as closing pop-up windows, closing prompt boxes, etc. When using jQuery, a popular JavaScript library, it becomes extremely simple and convenient to implement the close button event. This article will delve into how to use jQuery to implement close button events, and provide specific code examples to help readers better understand and master this technology. First, we need to understand how to define

JavaScript and WebSocket: Building an efficient real-time search engine JavaScript and WebSocket: Building an efficient real-time search engine Dec 17, 2023 pm 10:13 PM

JavaScript and WebSocket: Building an efficient real-time search engine Introduction: With the development of the Internet, users have higher and higher requirements for real-time search engines. When searching with traditional search engines, users need to click the search button to get results. This method cannot meet users' needs for real-time search results. Therefore, using JavaScript and WebSocket technology to implement real-time search engines has become a hot topic. This article will introduce in detail the use of JavaScript

See all articles