使用javascript获取鼠标位置、window.event(例子)
来源:互联网 发布:四季优美最大骗局 知乎 编辑:程序博客网 时间:2024/06/11 06:37
强烈推荐下面网站 http://www.webreference.com/ 。。。正在看这个页面的内容
http://www.webreference.com/programming/javascript/mk/column2/
上面的方法等都是抄出来的。。呵呵,一点一点学。。。
有两点东西要注意:
1.在IE下,
你加了个button,<input type="textbox" name="MouseYPosition" />
你用 document.getElementById(‘MouseYPosition’)可以得到这个button,但在Firefox下不行。
2.我在网上找了一下,不少人说在firefox中 event 只能在事件发生的现场使用,我不清楚是不是在IE下,window.event相当于全局变量,在firefox下则是局部变量?
http://www.webreference.com/programming/javascript/mk/column2/
<html>
<head>
<title>Get Mouse Position
</title>
<SCRIPT language="javascript" type="text/javascript">
function mouseCoords(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mouseCoords(ev);
document.getElementById('MouseXPosition').value = isNaN(mousePos.x) ? 0 : mousePos.x;
document.getElementById('MouseYPosition').value = isNaN(mousePos.y) ? 0 : mousePos.y;
}
document.onmousemove = mouseMove;
function doClick(e){
e = e || window.event;
var el = e.target || e.srcElement;
alert(el.name);
}
</SCRIPT>
</head>
<body>
<table border="1">
<tr>
<td>
X: <input type="textbox" id="MouseXPosition" name="MouseXPosition" size="4" />
</td>
<td>
Y: <input type="textbox" id="MouseYPosition" name="MouseYPosition" size="4" />
</td>
</tr>
<tr>
<td>
<input type="button" id="btn1" name="btn1" value="Button1" onclick="javascript:doClick(event);"/>
</td>
<td>
<input type="button" id="btn2" name="btn2" value="Button2" onclick="javascript:doClick(event);"/>
</td>
</tr>
</table>
</body>
</html>
<head>
<title>Get Mouse Position
</title>
<SCRIPT language="javascript" type="text/javascript">
function mouseCoords(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mouseCoords(ev);
document.getElementById('MouseXPosition').value = isNaN(mousePos.x) ? 0 : mousePos.x;
document.getElementById('MouseYPosition').value = isNaN(mousePos.y) ? 0 : mousePos.y;
}
document.onmousemove = mouseMove;
function doClick(e){
e = e || window.event;
var el = e.target || e.srcElement;
alert(el.name);
}
</SCRIPT>
</head>
<body>
<table border="1">
<tr>
<td>
X: <input type="textbox" id="MouseXPosition" name="MouseXPosition" size="4" />
</td>
<td>
Y: <input type="textbox" id="MouseYPosition" name="MouseYPosition" size="4" />
</td>
</tr>
<tr>
<td>
<input type="button" id="btn1" name="btn1" value="Button1" onclick="javascript:doClick(event);"/>
</td>
<td>
<input type="button" id="btn2" name="btn2" value="Button2" onclick="javascript:doClick(event);"/>
</td>
</tr>
</table>
</body>
</html>
上面的方法等都是抄出来的。。呵呵,一点一点学。。。
有两点东西要注意:
1.在IE下,
你加了个button,<input type="textbox" name="MouseYPosition" />
你用 document.getElementById(‘MouseYPosition’)可以得到这个button,但在Firefox下不行。
2.我在网上找了一下,不少人说在firefox中 event 只能在事件发生的现场使用,我不清楚是不是在IE下,window.event相当于全局变量,在firefox下则是局部变量?
- 使用javascript获取鼠标位置、window.event(例子)
- javascript获取鼠标相对于window的位置
- Javascript获取鼠标位置
- JavaScript-4.6鼠标事件监听,获取鼠标坐标window.event---ShinePans
- javascript获取鼠标的位置
- javascript 获取鼠标的位置
- Javascript获取鼠标位置及拖动函数
- 用javascript获取鼠标坐标位置
- javascript获取鼠标位置部分火狐,ie
- JavaScript 获取鼠标点击位置坐标
- 转 JavaScript 获取鼠标点击位置坐标
- JavaScript 获取鼠标点击位置坐标
- JavaScript 获取鼠标点击位置坐标
- JavaScript 获取鼠标点击位置坐标
- JavaScript 获取鼠标点击位置坐标
- JavaScript 获取鼠标点击位置坐标
- JavaScript 获取鼠标点击位置坐标
- JavaScript 获取鼠标点击位置坐标
- xmlHttpRequest对象的status属性的取值
- 梦
- Ubuntu Linux操作系统中的用户管理
- testfsafas
- Linux那些事儿之我是Hub(引子)
- 使用javascript获取鼠标位置、window.event(例子)
- 另一种鼠标双击事件
- 锁兼容性(数据库引擎)
- Firefox 使用技巧集
- 深圳的发展历程[图片]
- 求助
- 将ACCESS数据导入到SQL SERVER中
- 优秀创业者的人的基础
- 年轻的创业者们,行动起来吧