简体   繁体   English

浏览器控制台中的JavaScript事件对象

[英]JavaScript event object in the browser console

I apologize in advance for my ignorance. 我为自己的无知向您致歉。 I'm teaching myself JavaScript and currently experimenting with events, though I still getting confused with JS objects, I am not to sure what the console prints on the following event. 我正在自学JavaScript ,目前正在尝试事件,尽管我仍然对JS对象感到困惑,但是我不确定在下一个事件中控制台将输出什么。

element.onclick = function(objEvent) {
 Console.log(objEvent);
}

The console shows click clientX=76, clientY=20 . 控制台显示click clientX=76, clientY=20 What exactly is that information ? 那信息到底是什么? Are those properties of the event object ? 是事件对象的那些属性吗?

Are those properties of the event object ? 是事件对象的那些属性吗?

Yes, they are. 对,他们是。 In your case (for a click event) it's actually an object that's a superset of Event : MouseEvent . 在您的情况下(对于click事件),实际上是一个对象,它是Event的超集: MouseEvent

Whenever an event related to the DOM is fired, all of the relevant information about the action made is then gathered and stored on an object called event , which is, in your case, called objEvent . 每当触发与DOM相关的事件时 ,有关该动作的所有相关信息就会被收集并存储在一个名为event的对象上,在您的情况下,该对象称为objEvent

An event caused by a keyboard action generates information about the keys that were pressed . 由键盘操作引起的事件会生成有关已按下键的信息。 An event caused by the mouse , on the other hand, generates information about the mouse's position , which is your case here (the X and Y position of the mouse cursor). 另一方面, 由鼠标引起事件会生成有关鼠标位置的信息 ,这里就是您的情况(鼠标光标的XY位置)。

That's right. 那就对了。

From this event object there are various properties and methods. event object具有各种属性和方法。 The properties you are seeing there are the mouse positions. 您看到的属性是鼠标位置。

A common use of this event object might be to get the target / srcElement of the event 此事件对象的常见用法可能是获取事件的target / srcElement

event.target | event.srcElement

eg - get the id of the target/srcElement 例如-获取target / srcElement的ID

event.target.id

Very good object to study 很好的学习对象

Those are just the pixel coordinates on the screen of the place where your mouse clicked down. 这些只是鼠标单击位置的屏幕上的像素坐标。

clientX is the x coordinate clientX是x坐标

clientY is the y coordinate clientY是y坐标

Click is the event, clientX and clientY are the pixel coordinate location where the click happened. 单击是事件,clientX和clientY是单击发生的像素坐标位置。 Checkout this for more info - http://www.javascripter.net/faq/mouseclickeventcoordinates.htm 请查看此以获取更多信息-http: //www.javascripter.net/faq/mouseclickeventcoordinates.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM