0

这是我的编码。如果我单击并移动鼠标 onmousemove 事件应该发生。我怎样才能做到这一点。谁能帮帮我。

        </head>

        <body>
        <img id="myImgId" alt="" src="Chrysanthemum.jpg" width="400" height="300" />

        <script type="text/javascript">
        <!--
        var myImg = document.getElementById("myImgId");
        myImg.onmousemove = GetCoordinates;
        //-->
        </script>


        <p>X:<span id="x"></span></p>
        <p>Y:<span id="y"></span></p>
4

2 回答 2

0
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<img src="captain_america.jpg" style="left:0px;position:absolute;top:50px;"/>
<div id="showCoordinates" style="top:0px;left:0px;position:absolute;height:40px;width:100px"></div>
<script type="text/javascript">
document.addEventListener('mousemove', onMouseMove, false)

function onMouseMove(e)
{
    x = e.clientX;
    y = e.clientY;
    document.getElementById('showCoordinates').innerHTML = "x " + x + " y " + y; 
}
</script>
</body>
</html>
于 2013-05-30T07:25:51.323 回答
0

您必须在点击事件中启用 onmouosemove 事件

</head>

    <body>
    <img id="myImgId" alt="" src="Chrysanthemum.jpg" width="400" height="300" onClick="doSomething(this)"/>

    <script type="text/javascript">

  function doSomething(obj)
  {
    obj.onMouseMove = function getCoordinates(){
          var x= screen.getX;
          var y = screen.getY;
          }
  }
   </script>


    <p>X:<span id="x"></span></p>
    <p>Y:<span id="y"></span></p>
于 2013-05-30T07:25:26.100 回答