This article describes how to use JS events. Some attributes of events only make sense for specific events. For example, the fromElement and toElement attributes only make sense for onmouseover and onmouseout events.
JS event usage
Event indicates the event status, such as the element that triggers the event object, the location and status of the mouse, and the key to be pressed. The event object is valid only when an event occurs.
Some Properties of the event only make sense for specific events. For example, the fromElement and toElement attributes only make sense for onmouseover and onmouseout events.
The following example checks whether the mouse is clicked on the link. If the shift key is pressed, the link jump will be canceled.
- <HTML>
- <HEAD><TITLE>CancelsLinks</TITLE>
- <SCRIPTLANGUAGESCRIPTLANGUAGE="JScript">...
- functioncancelLink()...{
- if(window.event.srcElement.tagName=="A"&&window.event.shiftKey)
- window.event.returnValue=false;
- }
- </SCRIPT>
- <BODYonclickBODYonclick="cancelLink()">
-
The following example shows the current position of the mouse on the status bar.
- <BODYonmousemoveBODYonmousemove="window.status='X='+window.event.x+
- 'Y='+window.event.y">
Attribute:
- altKey,button,cancelBubble,clientX,clientY,
- ctrlKey,fromElement,keyCode,offsetX,offsetY,
- propertyName,returnValue,screenX,screenY,shiftKey,
- srcElement,srcFilter,toElement,type,x,y
1. altKey
Description:
Check the status of the alt key.
Syntax:
Event. altKey
Possible values:
When the alt key is pressed, the value is TRUE; otherwise, the value is FALSE. Read-only.
2. button
Description:
Check the pressed mouse key.
Syntax:
Event. button
Possible values:
0 without buttons
1 left click
2. Right-click
3. Press the Left or Right key.
4. Press the intermediate key
5. Press the left and intermediate keys.
6. Right-click and select intermediate key.
7. Press all keys
This attribute is only used for onmousedown, onmouseup, and onmousemove events. For other events, no matter the mouse status, 0 is returned, such as onclick ).
3. cancelBubble
Description:
Checks whether events of the upper layer are controlled.
Syntax:
Event. cancelBubble [= cancelBubble]
Possible values:
This is a read/write Boolean value:
TRUE is not controlled by the event of the parent layer.
FALSE allows the event to be controlled by the upper-layer element. This is the default value.
Example:
The following code snippet demonstrates the showSrc () function triggered by event onclick on the upper element body if the shift key is also pressed when onclick is clicked on the image.
- <SCRIPTLANGUAGESCRIPTLANGUAGE="JScript">
- functioncheckCancel()...{
- if(window.event.shiftKey)
- window.event.cancelBubble=true;
- }
- functionshowSrc()...{
- if(window.event.srcElement.tagName=="IMG")
- alert(window.event.srcElement.src);
- }
- </SCRIPT>
- <BODYonclickBODYonclick="showSrc()">
-
-
4. clientX
Description:
Returns the X coordinate of the mouse in the client area of the window.
Syntax:
Event. clientX
Note:
This is a read-only attribute. This means that you can only use it to get the current position of the mouse, but you cannot use it to change the position of the mouse.
5. clientY
Description:
Returns the Y coordinate of the mouse in the client area of the window.
Syntax:
Event. clientY
Note:
This is a read-only attribute. This means that you can only use it to get the current position of the mouse, but you cannot use it to change the position of the mouse.