tags: javascript
note:
Remove the right-click menu
document.body.addEventListener("contextmenu",clickHandler);
function clickHandler(e){
e.preventDefault();//Block event default behavior
console.log(e.type);
}
Prevent images from dragging by default
var img=document.querySelector("img");
img.addEventListener("mousedown",mouseHandler);
function mouseHandler(e){
e.preventDefault();
}
Prevent text dragging and selection
document.body.addEventListener("mousedown",mouseHandler);
function mouseHandler(e){
e.preventDefault();
}
Prevent form submission and reset
var bn=document.querySelector("[type=submit]");
bn.addEventListener("click",clickHandler);
function clickHandler(e){
e.preventDefault();
}
//Or write to form
var form=document.querySelector("form");
form.addEventListener("submit",submitHandler);
function submitHandler(e){
e.preventDefault();
//e.returnValue=false;//Compatible with IE8 and below
}
Print out the contents of the MouseEvent object:
document.body.addEventListener("mousedown",clickHandler);
function clickHandler(e){
console.log(e);
}
The print result is as follows (only part of the content is intercepted):

The following is the description of coordinate values:
The results printed in the following screenshots are the position coordinates of the upper left vertex of the div2 element.


When the element and its parent have no positioning attributes, the upper left corner of the body is used as the origin:

When the parent of the element has positioning attributes, use the upper left corner of the parent as the origin:

When the element itself has positioning attributes, the upper left corner of the element is used as the origin:



When a mouse event is triggered, an event parameter will be passed. In this parameter, we can get pageX/pageY, clientX/clientY, screenX/screenY, but what is the difference between these attributes? lo...
clientX set or get the mouse pointer positionRelative towindowThe x coordinates of the client area, where the client area does not include the controls and scroll bars of the window itself. clientY se...
screenX / Y from the mouse to the upper left corner of the monitor screen clientX / Y mouse to the visual browser page from the upper left corner (in the case where the value of the page no scroll bar...
clientY refers to the distance from the upper left corner of the visual page pageY refers to the distance from the upper left corner of the visual page (not affected by page scrolling) screenY refers ...
First, clientX, clientY Click position to the x, y coordinate of the current body visible area Second, pageX, pageY for the entire page, including the length of the body part that was rolled up Third,...
The following is the explanation of w3c about each attribute: PageX/Y is divided into jq: No official documentation for layerX/Y was found. to sum up: Recommended Use: Scree...
Event attribute layerX - compared to the position of the current coordinate system, that is, if the trigger element is not set to absolute positioning or relative positioning, the page is used as the ...
screenX, screenY: mouse relative to the user Display ScreenThe coordinates of the upper left corner; pageX, pageY: mouse relative FileThe coordinates of the upper left corner, pageY=clientY+scrollY cl...
【Please indicate the source】: We all know that several methods in the title are used to get the location of an event, but what is the difference between them? Just use a picture to illustrate offsetX:...
clientX, pageX, screenX, offsetX, layerX, x Native js: The console prints event object: chrome browser clientX/clientY: The coordinates of the relative viewable area (the viewable area, as ...