js中各浏览器中鼠标按键值的差异
发布时间:2024-05-05 09:15:28
W3C DOM-Level-2 定义如下
W3C DOM 写道
During mouse events caused by the depression or release of a mouse button, button is used to indicate which mouse button changed state. The values for button range from zero to indicate the left button of the mouse, one to indicate the middle button if present, and two to indicate the right button. For mice configured for left handed use in which the button actions are reversed the values are instead read from right to left.
其描述的很明确,0,1,2分别代表左,中,右三个键。以下分别在mousedown,mouseup,click,dbclick中测试。
<p id="p1">Test mousedown</p>
<p id="p2">Test mouseup</p>
<p id="p3">Test click</p>
<p id="p4">Test dbclick</p>
<script type="text/javascript">
function $(id){return document.getElementById(id)}
var p1 = $('p1'), p2 = $('p2'), p3 = $('p3'), p4 = $('p4');
p1.onmousedown = function(e){
e = window.event || e;
alert(e.button);
}
p2.onmouseup = function(e){
e = window.event || e;
alert(e.button);
}
p3.onclick = function(e){
e = window.event || e;
alert(e.button);
}
p4.ondbclick = function(e){
e = window.event || e;
alert(e.button);
}
</script>
即:
IE6/7/8中,mousedown/mouseup 事件中获取左键的值为1,click事件中获取的却是0。
其它浏览器,mousedown/mouseup/click 事件中获取左键值均为0。完全遵循标准。
所有浏览器,dbclick事件中均无法获取
即:
IE6/7/8中,mousedown/mouseup 事件中获取中键的值为4。
IE6/7中,click事件无法获取中键的值。IE8则可以,但值为0。
Firefox3.6/Chrome7/Safari5中,mousedown/mouseup 事件中获取中键值为1。
Chrome7/Safar5中,click事件也能获取中键值,亦为1。
Opera10中无法获取中键值。
即:
所有浏览器,mousedown/mouseup事件中均能获取右键值,且都为2。
所有浏览器,click/dbclick事件中均不能获取到右键值。
以上可看到,判断鼠标按下了哪个键 ,应该选择合适的事件 。这里应选mousedown/mouseup。Opera10中仍然无法获取到中键的值,因为Opera压根不触发中键的事件(mousedown,mouseup,click,dbclick)。
以下代码将IE6/7/8的值转换成符合W3C标准的
var ie678 = !-[1,];
function getButton(e){
var code = e.button;
var ie678Map = {
1 : 0,
4 : 1,
2 : 2
}
if(ie678){
return ie678Map[code];
}
return code;
}
猜你喜欢
- 本文实例讲述了Python开发中爬虫使用代理proxy抓取网页的方法。分享给大家供大家参考,具体如下:代理类型(proxy):透明代理 匿名
- 连接数据库链接数据库需要提供一个地址和接口即可。首先还是要导入包。from pymongo import MongoClientconn =
- mysql对列求和mysql中,可以使用SELECT语句配合SUM()函数来对列求和,能够返回指定列值的总和,求和语法为&ldquo
- 前言本项目主要通过python的matplotlib pandas pyecharts等库对疫情数据进行可视化分析数据来源:本数据集来源于k
- ansible 简介ansible 是什么?ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、
- 本文实例讲述了基于Python开发chrome插件的方法。分享给大家供大家参考,具体如下:谷歌Chrome插件是使用HTML、JavaScr
- 顾名思义,本期内容肯定是涉及编程时间,那在操作python要怎么用time这个方法呢?一起来看下吧~时间模块的定义与使用:时间模块time是
- 我一直建议每个开发者都要有写博客记笔记的习惯,一来可以沉淀知识,二来可以帮助别人,我使用过很多博客平台,也用Python开发过博客系统,就这
- 效果如图 一、获取天气def getWeather1(city): try:
- 一、背景(正)地理编码指的是:将地理位置名称转换成经纬度;逆地理编码指的是:将经纬度转换成地理位置信息,如地名、所在的省份或城市等百度地图提
- 此文用来正式回复大辉同学的疑问。1、结论:固定宽度只适合功能型网站,不适合希望用户认真阅读的浏览型网站。UCD大社区是浏览型网站,它的定位是
- 1.1 什么是Mysql多实例?简单的说,Mysql多实例就是在一台服务器上同时开启多个不同的服务端口(如 : 3306/3307/3308
- 牛顿法求平方根原理计算机常用循环来计算F的平方根.从某个猜测的x值开始,根据x^2与F的近似度来调整x,产生一个更好的猜测:x -= (x
- 一、概述OLAP的系统(即Online Aanalyse Process)一般用于系统决策使用。通常和数据仓库、数据分析、数据挖掘等概念联系
- 下面的代码主要用于使用python语言调用NASA官方的MODIS处理工具HEG进行投影坐标转换与重采样批量处理主要参考HEG的用户手册:h
- 用MSI安装包安装根据自己的操作系统下载对应的32位或64位安装包。按如下步骤操作:MySQL数据库官网的下载地址http://dev.my
- 最近在上《自然语言处理》这门选修课,为了完成上机作业也是很认真了,这次是为了实现语角色标注任务,于是就入了这个坑,让我们来(快乐地 )解决出
- 获取要爬取的URL爬虫前期工作用Pycharm打开项目开始写爬虫文件字段文件items# Define here the models fo
- 第一步:使用记事本打开mysql安装目录下的"my.ini”文件。# MySQL client library initializ
- Oracle公司6月9日宣布同意收购TimesTen公司,TimesTen是一家私营软件企业,其产品能提高用于股市和机票预订等需要快速响应时