search
HomeWeb Front-endJS TutorialDetailed explanation of js events. _javascript skills

event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等。
event对象只在事件发生的过程中才有效。
event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 onmouseover 和 onmouseout 事件有意义。
例子
下面的例子检查鼠标是否在链接上单击,并且,如果shift键被按下,就取消链接的跳转。
code

Cancels Links
<script> <BR>function cancelLink() { <BR> if (window.event.srcElement.tagName == "A" && window.event.shiftKey) <BR> window.event.returnValue = false; <BR>} <BR></script>

下面的例子在状态栏上显示鼠标的当前位置。
code
属性:
altKey, button, cancelBubble, clientX, clientY, ctrlKey, fromElement, keyCode, offsetX, offsetY, propertyName, returnValue, screenX, 
screenY, shiftKey, srcElement, srcFilter, toElement, type, x, y
1.altKey
描述:
检查alt键的状态。
语法:
event.altKey
可能的值:
当alt键按下时,值为 TRUE ,否则为 FALSE 。只读。
2.button
描述:
检查按下的鼠标键。
语法:
event.button
可能的值:
0 没按键
1 按左键
2 按右键
3 按左右键
4 按中间键
5 按左键和中间键
6 按右键和中间键
7 按所有的键
这个属性仅用于onmousedown, onmouseup, 和 onmousemove 事件。对其他事件,不管鼠标状态如何,都返回 0(比如onclick)。
3.cancelBubble
描述:
检测是否接受上层元素的事件的控制。
语法:
event.cancelBubble[ = cancelBubble]
可能的值:
这是一个可读写的布尔值:
TRUE 不被上层原素的事件控制。
FALSE 允许被上层元素的事件控制。这是默认值。
例子:
下面的代码片断演示了当在图片上点击(onclick)时,如果同时shift键也被按下,就取消上层元素(body)上的事件onclick所引发的showSrc()函数。
code<script> <BR>function checkCancel() { <BR> if (window.event.shiftKey) <BR> window.event.cancelBubble = true; <BR>} <BR>function showSrc() { <BR> if (window.event.srcElement.tagName == "IMG") <BR> alert(window.event.srcElement.src); <BR>} <BR></script>

Detailed explanation of js events. _javascript skills

4.clientX
描述:
返回鼠标在窗口客户区域中的X坐标。
语法:
event.clientX
注释:
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。
5.clientY
描述:
返回鼠标在窗口客户区域中的Y坐标。
语法:
event.clientY
注释:
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。
6.ctrlKey
描述:
检查ctrl键的状态。
语法:
event.ctrlKey
可能的值:
当ctrl键按下时,值为 TRUE ,否则为 FALSE 。只读。
7.fromElement
描述:
检测 onmouseover 和 onmouseout 事件发生时,鼠标所离开的元素。 参考:18.toElement
语法:
event.fromElement
注释:
这是个只读属性。
8.keyCode
描述:
检测键盘事件相对应的内码。
这个属性用于 onkeydown, onkeyup, 和 onkeypress 事件。
语法:
event.keyCode[ = keyCode]
可能的值:
这是个可读写的值,可以是任何一个Unicode键盘内码。如果没有引发键盘事件,则该值为 0 。
9.offsetX
描述:
检查相对于触发事件的对象,鼠标位置的水平坐标
语法:
event.offsetX
10.offsetY
描述:
检查相对于触发事件的对象,鼠标位置的垂直坐标
语法:
event.offsetY
11.propertyName
描述:
设置或返回元素的变化了的属性的名称。
语法:
event.propertyName [ = sProperty ]
可能的值:
sProperty 是一个字符串,指定或返回触发事件的元素在事件中变化了的属性的名称。
这个属性是可读写的。无默认值。
注释:
你可以通过使用 onpropertychange 事件,得到 propertyName 的值。
例子:
下面的例子通过使用 onpropertychange 事件,弹出一个对话框,显示 propertyName 的值。
code
<script> <BR>function changeProp(){ <BR> btnProp.value = "This is the new VALUE"; <BR>} <BR>function changeCSSProp(){ <BR> btnStyleProp.style.backgroundColor = "aqua"; <BR>} <BR></script>


The event object property propertyName is
used here to return which property has been
altered.


VALUE="Click to change the VALUE property of this button"
onpropertychange='alert(event.propertyName+" property has changed value")'>
onclick="changeCSSProp()"
VALUE="Click to change the CSS backgroundColor property of this button"
onpropertychange='alert(event.propertyName+" property has changed value")'>


12.returnValue
描述:
设置或检查从事件中返回的值
语法:
event.returnValue[ = Boolean]
可能的值:
true 事件中的值被返回
false 源对象上事件的默认操作被取消
例子见本文的开头。
13.screenX
描述:
检测鼠标相对于用户屏幕的水平位置
语法:
event.screenX
注释:
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。
14.screenY
描述:
检测鼠标相对于用户屏幕的垂直位置
语法:
event.screenY
注释:
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。
15.shiftKey
描述:
检查shift键的状态。
语法:
event.shiftKey
可能的值:
当shift键按下时,值为 TRUE ,否则为 FALSE 。只读。
16.srcElement
描述:
返回触发事件的元素。只读。例子见本文开头。
语法:
event.srcElement
17.srcFilter
描述:
返回触发 onfilterchange 事件的滤镜。只读。
语法:
event.srcFilter
18.toElement
描述:
检测 onmouseover 和 onmouseout 事件发生时,鼠标所进入的元素。 参考:7.fromElement
语法:
event.toElement
注释:
这是个只读属性。
例子:下面的代码演示了当鼠标移到按钮上时,弹出一个对话框,显示“mouse arrived”
code<script> <BR>function testMouse(oObject) { <BR> if(oObject.contains(event.toElement)) { <BR> alert("mouse arrived"); <BR> } <BR>} <BR></script>:

19.type
描述:
返回事件名。
语法:
event.type
注释:
返回没有“on”作为前缀的事件名,比如,onclick事件返回的type是click
只读。
20. x
描述:
返回鼠标相对于css属性中有position属性的上级元素的x轴坐标。如果没有css属性中有position属性的上级元素,默认以BODY元素作为参考对象。
语法:
event.x
注释:
如果事件触发后,鼠标移出窗口外,则返回的值为 -1
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。
21. y
描述:
返回鼠标相对于css属性中有position属性的上级元素的y轴坐标。如果没有css属性中有position属性的上级元素,默认以BODY元素作为参考对象。
语法:
event.y
注释:
如果事件触发后,鼠标移出窗口外,则返回的值为 -1
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。
Statement
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool