An event represents the state of an incident, such as the element that triggered the object, the position and state of the mouse, the key pressed, and so on.
The event object is only valid during the events.
Some properties of an event are only meaningful for a particular event. For example, the Fromelement and Toelement properties are only meaningful for onmouseover and onmouseout events.
Example
The following example checks if the mouse is clicked on the link and, if the SHIFT key is pressed, cancels the linked jump.
<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Headrunat= "Server"><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <title></title> <Scripttype= "Text/javascript"> functionCancellink () {if(Window.event.srcElement.tagName== "A" &&Window.event.shiftKey) Window.event.returnValue= false; } </Script></Head><Bodyonclick= "Cancellink ()"> <formID= "Form1"runat= "Server"> <Div> <inputtype= "text"ID= "a"onkeyup= "tt ()"/> </Div> </form></Body></HTML>
The following example displays the current position of the mouse on the status bar.
<onmousemove= "Window.status = ' x= ' + window.event.x + ' y= ' + window.event.y" >
Property:
Altkey, Button, cancelbubble, ClientX, ClientY, Ctrlkey, Fromelement, KeyCode, OffsetX, OffsetY, PropertyName, Returnvalu E, ScreenX,
ScreenY, Shiftkey, srcelement, Srcfilter, toelement, type, x, y
1.altkey
Description: Checks the state of the ALT key.
Syntax: Event.altkey
Possible values: When the ALT key is pressed, the value is TRUE, otherwise FALSE. Read-only.
2.button
Description: Check the mouse button that was pressed.
Syntax: Event.button
Possible values:
0 No buttons.
1 Press the left button
2 Press the right button
3 Press the left and right key
4 Press the Middle key
5 Press the left and middle keys
6 Right-click and Middle-key
7 Press All the keys
This property is only used for onmousedown, onmouseup, and onmousemove events. Returns 0 (for example, onclick) for other events, regardless of mouse state.
3.cancelbubble
Description: Detects whether to accept control over events of the upper element.
Syntax: event.cancelbubble[= cancelbubble]
Possible values:
This is a read-write Boolean value:
TRUE is not controlled by the upper primitive's events.
FALSE allows events to be controlled by the upper element. This is the default value.
Example:
The following code snippet shows the SHOWSRC () function that is thrown by the event onclick on the upper element (body) If the SHIFT key is pressed at the same time when clicking on the image (onclick).
<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> <title></title> <Script> functionCheckcancel () {if(Window.event.shiftKey) window.event.cancelBubble= true; } functionshowsrc () {if(Window.event.srcElement.tagName== "IMG") alert (WINDOW.EVENT.SRCELEMENT.SRC); } </Script></Head><Bodyonclick= "showsrc ()"> <imgonclick= "Checkcancel ()"src= "/sample.gif"></Body></HTML>
4.ClientX
Description: Returns the x-coordinate of the mouse in the window client area.
Syntax: Event.clientx
Note: This is a read-only property. This means that you can only use it to get the current position of the mouse, but it cannot be used to change the position of the mouse.
5.ClientY
Description: Returns the y-coordinate of the mouse in the window client area.
Syntax: Event.clienty
Note: This is a read-only property. This means that you can only use it to get the current position of the mouse, but it cannot be used to change the position of the mouse.
6.Ctrlkey
Description: Checks the state of the CTRL key.
Syntax: Event.ctrlkey
Possible values: When the CTRL key is pressed, the value is TRUE, otherwise FALSE. Read-only.
7.fromelement
Description: Detects onmouseover and onmouseout events that occur when the mouse leaves the element. Reference: 18.toElement
Syntax: event.fromelement
Note: This is a read-only property.
8.keycode
Description: Detects the internal code corresponding to the keyboard event. This property is used for onkeydown, onkeyup, and onkeypress events.
Syntax: event.keycode[= keycode]
Possible values: This is a read-write value, which can be any Unicode keyboard inside code. If no keyboard event is raised, the value is 0.
9.OffsetX
Description: Checks the horizontal coordinates of the mouse position relative to the object that triggered the event.
Syntax: Event.offsetx
Ten.OffsetY
Description: Check the vertical coordinates of the mouse position relative to the object that triggered the event
Syntax: event.offsety
PropertyName .
Description: Sets or returns the name of the property that changed the element.
Syntax: event.propertyname [= Sproperty]
Possible value: Sproperty is a string that specifies or returns the name of the property that has changed in the event of the element that triggered the event. This property is readable and writable. No default value.
Note: You can get the value of PropertyName by using the Onpropertychange event.
Example: The following example shows the value of PropertyName by using the Onpropertychange event, which pops up a dialog box.
<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head> <Scripttype= "Text/javascript"> functionChangeprop () {Btnprop.value= "This is the new VALUE"; } functionChangecssprop () {BtnStyleProp.style.backgroundColor= "Aqua"; } </Script></Head><Body> <P>The Event object property PropertyName isused-here-return which property has been altered. </P> <inputtype= "button"ID= "Btnprop"onclick= "Changeprop ()"value= " Click to change"Onpropertychange= "alert (event.propertyname)"> <inputtype= "button"ID= "Btnstyleprop"onclick= "Changecssprop ()"value= " Click to change the CSS"Onpropertychange= "alert (event.propertyname)"></Body></HTML>
returnvalue .
Description: Sets or checks the value returned from the event
Syntax: event.returnvalue[= Boolean]
Possible values: The value in the true event is returned, false the default action of the event on the source object is canceled, as shown at the beginning of this article.
ScreenX .
Description: Detects the horizontal position of the mouse relative to the user's screen
Syntax: Event.screenx
Note: This is a read-only property. This means that you can only use it to get the current position of the mouse, but it cannot be used to change the position of the mouse.
ScreenY .
Description: Detects the vertical position of the mouse relative to the user's screen
Syntax: Event.screeny
Note: This is a read-only property. This means that you can only use it to get the current position of the mouse, but it cannot be used to change the position of the mouse.
shiftkey .
Description: Checks the state of the SHIFT key.
Syntax: Event.shiftkey
Possible values: When the SHIFT key is pressed, the value is TRUE, otherwise FALSE. Read-only.
srcelement .
Description: Returns the element that triggered the event. Read-only. See the beginning of this article for examples.
Syntax: event.srcelement
srcfilter .
Description: Returns the filter that triggered the onfilterchange event. Read-only.
Syntax: Event.srcfilter
toelement .
Description: Detects the elements that the mouse enters when the onmouseover and onmouseout events occur. Reference: 7.fromElement
Syntax: event.toelement
Note: This is a read-only property.
Example: The following code shows a dialog box that shows "mouse arrived" when you move the mouse over the button
<Script> functionTestmouse (oobject) {if(Oobject.contains (event.toelement)) {alert ("Mouse arrived"); } }</Script><ButtonID= "Obutton"onmouseover= "Testmouse (this)">Mouse over this.</Button>
.Type
Description: Returns the event name.
Syntax: Event.type
Note: Returns an event name that does not have "on" as the prefix, for example, the type returned by the OnClick event is click Read-only.
x. x
Description: Returns the x-coordinate of the mouse relative to the ancestor element with the Position property in the CSS property. If there is no ancestor element with the Position property in the CSS property, the BODY element is used as the reference object by default.
Syntax: event.x
Note: If the mouse moves out of the window after the event is triggered, the value returned is-1, which is a read-only property. This means that you can only use it to get the current position of the mouse, but it cannot be used to change the position of the mouse.
. y
Description: Returns the y-coordinate of the mouse relative to the ancestor element with the Position property in the CSS property. If there is no ancestor element with the Position property in the CSS property, the BODY element is used as the reference object by default.
Syntax: EVENT.Y
Note: If the mouse moves out of the window after the event is triggered, the value returned is-1, which is a read-only property. This means that you can only use it to get the current position of the mouse, but it cannot be used to change the position of the mouse.
JS of the event detailed