Home  >  Article  >  Web Front-end  >  Native js implements cross-browser acquisition of mouse button value_javascript skills

Native js implements cross-browser acquisition of mouse button value_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:37:52846browse
Copy code The code is as follows:

document.onmousedown = function( e ){
alert(getButton (e)) // e.button W3C is to get the mouse button 0 means left button 1 means middle button 2 means right button and IE browser is 1 means left button 4 means middle 2 means right button The IE browser here is mainly IE8 or below browser
};
function getButton(e){
/*
1. The window.event attribute is supported by IE and Chrome
2. But Chrome also supports W3C
3. Therefore, if both W3C and IE support it, then W3C has been standardized
*/
if( e ){ // As the first judgment, Chrome will use W3C as the standard.
return e.button;
}else if( window.event ){
switch( window.event.button ){
case 1 : return 0; // Return the value of the left mouse button
case 4 : return 1; // Return the value of the middle mouse button
case 2 : return 2; // Return the value of the right mouse button
case 0 : return 2; // Return the value of the right mouse button mainly The 360 ​​browser will return the 0 returned in the IE browser, which represents the value returned when the mouse button is not pressed.
};
};
};
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