onClick
クリックすると(イベント)、関数が実行される小さなdivタグがありprintMousePos()
ます。これはHTML
タグです:
<html>
<header>
<!-- By the way, this is not the actual html file, just a generic example. -->
<script src='game.js'></script>
</header>
<body>
<div id="example">
<p id="test">x: , y:</p>
</div>
</body>
</html>
これは、別の .js ファイル内の printMousePos 関数です。
function printMousePos() {
var cursorX;
var cursorY;
document.onmousemove = function(e){
cursorX = e.pageX;
cursorY = e.pageY;
}
document.getElementById('test').innerHTML = "x: " + cursorX + ", y: " + cursorY;
}
はい、関数は実際に機能します (いつクリックしたかを認識します) が、x と y の両方に対して undefined を返すため、関数内の x と y の取得コードが正しくないと想定しています。何か案は?私はまた、Javaのようにxとyを返すためにjavascript自体に組み込み関数がないことも知っています。例えば、JQueryやphpでそれを行う方法はありますか? (ただし、可能であればそれらを避けてください。javascriptが最適です)。ありがとう!