js获得鼠标的位置

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- 
 2 transitional.dtd"> 
 3 <html xmlns="http://www.w3.org/1999/xhtml"> 
 4 <head> 
 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
 6 <title>JS获得鼠标位置</title> 
 7 </head> 
 8 
 9 <body> 
10 
11 <script> 
12 function mouseMove(ev) 
13 { 
14 ev= ev || window.event; 
15 var mousePos = mouseCoords(ev); 
16 //alert(ev.pageX); 
17 document.getElementById("xxx").value = mousePos.x; 
18 document.getElementById("yyy").value = mousePos.y; 
19 } 
20 
21 function mouseCoords(ev) 
22 { 
23 if(ev.pageX || ev.pageY){ 
24 return {x:ev.pageX, y:ev.pageY}; 
25 } 
26 return { 
27 x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
28 y:ev.clientY + document.body.scrollTop - document.body.clientTop 
29 }; 
30 } 
31 
32 document.onmousemove = mouseMove; 
33 </script> 
34 鼠标 X 轴: 
35 <input id=xxx type=text> 
36 
37 鼠标 Y 轴: 
38 <input id=yyy type=text> 
39 </body> 

 

posted @ 2013-07-05 15:51  RightDear  阅读(382)  评论(0编辑  收藏  举报