代码:
<html>
<head>
<title></title>
<script>
function show(){
document.getElementById("x").value=event.clientX;
meter1=setTimeout("show()",2000);
}</script>
</head>
<body onmousemove="show()">
<input type="text" id="x"  value="坐标x"/>
<input type="button" id="G" value="停止坐标" onclick="clearTimeout(meter1)"/></body>
</html>
我就想让输入框显示鼠标的水平坐标,能显示,但是随后就谈错,不清楚这是为什么,求指正,
友情提示,别运行,否则就准备ALT+F4关闭浏览器了

解决方案 »

  1.   

    setTimeout到时执行函数的时候event参数就消失了,event只有触发事件时才有效。
    另外要兼容firefox还得修改一些细节。
    <html>
    <head>
    <title></title>
    <script>
    function show(e) {
    e = e || event;
    document.getElementById("x").value = e.clientX;
    }
    </script>
    </head>
    <body onmousemove="show(event)">
    <input type="text" id="x" value="坐标x"/>
    <input type="button" id="G" value="停止坐标" onclick="window.onmousemove=document.body.onmousemove=null;"/>
    <input type="button" id="G2" value="开始坐标" onclick="window.onmousemove=document.body.onmousemove=show;"/>
    </body>
    </html>
      

  2.   


    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test</title>
    <script>
    function show(e){
    document.getElementById("x").value=e.clientX;
    meter1=setTimeout("show(event)",2000);
    }</script>
    </head><body onmousemove="show(event)">
    <input type="text" id="x" value="坐标x"/>
    <input type="button" id="G" value="停止坐标" onclick="clearTimeout(meter1)"/>
    </body>
    </html>