JavaScript移動時にマウスの座標を取得します(IE8、Google、Firefox、Operaと互換性があり、テストに合格します
HTMLファイルに直接コピーして実行します。
みんなのテストを容易にするために、オンラインデモが準備されました
<!doctype html public " - // w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> javascriptは、マウスが動くと座標を取得します(互換性:IE8、Google、Google、firefox、Opera)_wulin.com </title> <meta http-equiv = "content-thepe" charset = utf-8 " /> <style type =" text /css "> .tip {width:200px;ボーダー:2px solid #ddd;パディング:8px;背景:#f1f1f1;色:#666; } </style> <script type = "text/javascript"> // method 1 function mousepos(e){var x、y; var e = e || window.event; return {x:e.clientx + document.body.scrollleft + document.documentlement.scrollleft、y:e.clienty + document.body.scrolltop + document.documentlement.scrolltop}; }; //方法2 // firefoxは、属性とpageyプロパティをサポートしています。これらの2つのプロパティには、ページスクロールが含まれています。 // Chromeでは、ページのスクロール変位をdocument.body.scrollleft、document.body.scrolltop、およびIEで計算できます。DocumenteLement.documenteleft、document.documentelement.scrolltop関数getMousePos(var e = event || window.event; var scrollx = document.documentelement.scrollleft || document.body.scrollleft; var scrolly = document.documentelement.scrolltop || document.body.scrolltop; var x = e.pagex || e.clientx + scrollx; var y = e.pagey || E.Clienty + Scrolly; // alert( 'x:' + x + '/ny:' + y); {'x':x、 'y':y}を返します。 } function test(e){document.getElementById( "mjs")。innerhtml = getMousePos(e).x+'、'+getMousePos(e).y; }; </script> </head> <body> <div id = "mjs">マウスクリック位置座標を取得</div> <div id = "test" onmousemove = "test(event)"> </div> </body> </html> </html>