Home Web Front-end JS Tutorial JavaScript Event Learning Chapter 7 Event Properties_Javascript Skills

JavaScript Event Learning Chapter 7 Event Properties_Javascript Skills

May 16, 2016 pm 06:35 PM
event

When we want to read some information about Event, we are often buried in a large number of attributes, most of which do not run well in most browsers. Here is the event compatibility list.
I am not going to make a list of these attributes, because those situations are too confusing, and it will not be helpful to your learning at all. Before writing the 5 pieces of code, I want to ask 5 questions about the browser.
1. What is the type of event?
2. Which HTML element is the target of the event?
3. Which keys were pressed when the event occurred?
4. Which mouse button was pressed when the Event occurred?
5. Where is the mouse position when the Event occurs?
I have given a very detailed answer to the last question here.
Please note that I have done very strict object checking on these codes. I first created cross-browser access to the event, and then checked for browser support before using each attribute.

1. What is the type of event?
This is a cross-browser question with a standard answer: Use the type attribute to view its properties:

Copy code The code is as follows:

function doSomething(e) {
if (!e) var e = window.event;
alert(e.type);
}



2. Which HTML element is the target of the event?
W3C/Netscape said: target. No, Microsoft said, it's srcElement. Both properties return the HTML element when the event occurred.
Copy code The code is as follows:

function doSomething(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
}


The last two lines of code are specifically for Safari. If an event occurs on an element that contains text, the text node becomes the target of the event rather than the element itself. So we want to check if the target's nodetype is 3 (text node). If it is, we move it to the parent node, the HTML element.
Even if the event is captured or bubbles up (bubbles up), the target/srcElement attribute is still the element where the event occurred earliest.
Other targets
There are many targeting attributes. I discussed currentTarget in the article Event Order and relatedTarget, fromElement and toElement in the article Mouse event.


3. Which keys were pressed when the event occurred?
This question is relatively simple. First get the code of the key (a=65) from the keyCode attribute. After you get the key value, you can know the actual key value through the String.fromCharCode() method, if necessary.
Copy code The code is as follows:

function doSomething(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
alert('Character was ' character);
}


There are some places here that may make keyboard events difficult to use. For example, the kepress event fires as long as the user presses the key. However, the triggering time of keydown in most browsers is as long as the pressing time. I'm not sure if that's a good idea, but that's what it is.


4. Which mouse button was pressed when the Event occurred?
There are two properties here to know which mouse button was pressed: which and button. Please note that these properties generally do not necessarily work on click. To safely detect which mouse button was pressed, you'd better use the mousedown and mouseup events.
which is an old Netscape property. The value of the left mouse button is 1, the value of the middle button (scroll wheel) is 2, and the value of the right mouse button is 3. There are no problems except for the weak support. In fact, it is often used to detect mouse buttons.
Button attributes are now well recognized. W3C's standard values ​​are as follows:
Left button 0
Middle button 1
Right button 2
Microsoft's standard values ​​are as follows:
Left button 1
Middle button 4
Right button 2
There is no doubt that Microsoft’s standards are better than W3C’s. 0 can mean that no key is pressed, and everything else is unreasonable.
In addition, only in the Microsoft model, the values ​​of the buttons can be combined. For example, 5 means that the "left button and middle button" are pressed together. Not only does IE6 not support merging, but the w3c model is theoretically impossible: you never know whether the left button was pressed.
So in my opinion w3c has made a serious mistake in defining buttons.

Right Click
Fortunately, usually you want to know if the right button was clicked. Because W3C and Microsoft happen to define the button value as 2 on this issue, you can still detect right clicks.
Copy code The code is as follows:

function doSomething(e) {
var rightclick;
if (!e) var e = window.event;
if (e.which) rightclick = (e.which == 3);
else if (e.button) rightclick = (e. button == 2);
alert('Rightclick: ' rightclick); // true or false
}


It should be noted that Macs usually only have one button, Mozilla defines the value of the Ctrl-Click button as 2, so Ctrl-Click will also open the menu. ICab does not yet support mouse button attributes, so you cannot detect right clicks in Opera.
5. Where is the mouse position when the Event occurs?
The problem of mouse position is quite serious. Although there are no less than 6 properties for mouse coordinates, there is still no reliable cross-browser method to find the mouse coordinates.
The following are these 6 sets of coordinates:
1. clientX, clientY
2. layerX, layerY
3. offsetX, offsetY
4. pageX, pageY
5. screenX, screenY
6. x, y
I once explained the issues of pageX/Y and clientX/Y here.
screenX and screenY are the only pair of properties that are cross-browser compatible. They give the coordinates of the mouse across the entire computer screen. Unfortunately, this information alone is not enough: you never need to know where the mouse is on the screen - well, maybe you want to place a new window at the current mouse position.
The other three pairs of attributes are not important, see the description here.
Correct code
The following code can correctly detect the coordinates of the mouse
Copy the code The code is as follows:

function doSomething(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e .pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX document.body.scrollLeft
document.documentElement.scrollLeft;
posy = e.clientY document.body.scrollTop
document.documentElement.scrollTop;
}
// posx and posy contain the mouse position relative to the document
// Do something with this information
}


Original text here: http://www.quirksmode.org /js/events_properties.html

Please give me some advice on my twitter: @rehawk
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

Video Face Swap

Video Face Swap

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

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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles