您的位置:首页 > Web前端

Mouse Buttons on different Browsers

2010-06-11 11:41 211 查看
Deals with the fact that mouse buttons are referenced in different ways
by different browsers.

 

// Browser detection
var ie=document.all != null;  //ie4
var op7=navigator.userAgent.indexOf("opera")>0 && operaVersion() <= 7;
function operaVersion() {
agent = navigator.userAgent;
idx = agent.indexOf("opera");
if (idx>-1) {
return parseInt(agent.subString(idx+6,idx+7));
}
}

/* Detection of the mouse button
L M R
IE,KON		1 4 2  event.button
NS,OP8,FF	0 1 2  e.button
OP7 		1 3 2  e.button
NS,OP8,FF	1 2 3  e.which
*/
var leftButton   = ie? 1 : 0; // op7 supports document.all
var middleButton = op7 ? 3 : ie ? 4 : 1;
var rightButton  = 2;
document.onmouseup = onClick;
// This code is executed each time a mouse button is released
function onClick(e) {
if (ie) {
var elem = event.srcElement;
var btn  = event.button;
//e = event;
} else {
var elem = e.target;
var btn  = e.button;
}
// elem is the element the user clicked on
// btn is the mouse button which was used
// e.g. if (btn == leftButton) { alert( elem + ": You clicked me!" ); }

/* ...your code goes here... */

return false;
}


 

原帖地址: http://snipplr.com/view/275/mouse-buttons-on-different-browsers/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息