سؤال

i would like to know how can i get x,y coordinate on every click, in every place on the page. I want to use the div's but i dont know how to modify it. Thanks. It only shows x,y coordinates whille i click the pharagraph. this is my code:

<script>
 function show_coords(event)
 {
 var x=event.clientX;
 var y=event.clientY;
 alert("X coords: " + x + ", Y coords: " + y);
 }
 </script>

 <div id="x">x</div>
 <div id="y">y</div>
 <p onmousedown="show_coords(event)">Click this paragraph, and an alert box will alert the x and y coordinates of the mouse pointer.</p>
هل كانت مفيدة؟

المحلول

Add the same event to body

<body onmousedown="show_coords(event)">
....
</body>

To elaborate more for question owner on why it does not work on entire "window" yet: Your body is not covering the entire window. Just the div element and that is why you stll get same result. make the body to occupy full window. See below sample:-

<html>
<head>
<title>sample</title>
<style>
body {
 border : 2px solid red;
 overflow : true;
 width : 100% ;
 height : 100%;
}
</style>
<script type="text/javascript">
function show_coords(event) {
 var x=event.clientX;
 var y=event.clientY;
 alert("X coords: " + x + ", Y coords: " + y);
}
</script>
</head>
<body onmousedown="show_coords(event)">
</body>
</html>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top