这是我的编码。如果我点击并移动鼠标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>
答案 0 :(得分: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>
答案 1 :(得分: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>