0
<script type="text/javascript">
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMousePos;
// Temporary variables to hold mouse x-y pos.s
var tempX = 30;
var tempY = 30;

function getMousePos(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch/correct negative values 
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  

  // show the position values in the form
  document.MouseDisplay.MouseX.value = tempX;
  document.MouseDisplay.MouseY.value = tempY;
  return true;
}

// execute function
if (tempY < 30) {
 alert('tempY is found to be less than 30');
}

</script>

このコードはhotscriptとcodelifterhttp ://www.codelifter.com/main/javascript/capturemouseposition1.htmlで見つけました。これは古いスレッドからのものですが、達成する必要があるものがあると思います。マウスのY位置が30未満のときはいつでも関数を実行したい。

私はプログラミングの初心者です。ここのサンプルからゆっくりと学べることを願っています。

私の質問は、なぜアラートコマンドがトリガーされないのですか?ここで何が欠けていますか。簡単なボタンを押して、機能する関数を呼び出しました。

<script type="text/javascript">
function myFunction() {
alert('tempY is found to be less than 30');
}
</script>
4

1 に答える 1

0

コードを試したことはありませんが、マウスを動かしたときに機能すると仮定して、アラートを確認するには、関数内でその行を移動するだけです。

var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMousePos;
// Temporary variables to hold mouse x-y pos.s
var tempX = 30;
var tempY = 30;

    function getMousePos(e) {
      if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.body.scrollLeft
        tempY = event.clientY + document.body.scrollTop
      } else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX
        tempY = e.pageY
      }  
      // catch/correct negative values 
      if (tempX < 0){tempX = 0}
      if (tempY < 0){tempY = 0}  

      // show the position values in the form
      document.MouseDisplay.MouseX.value = tempX;
      document.MouseDisplay.MouseY.value = tempY;

      // execute function
      if (tempY < 30) {
       alert('tempY is found to be less than 30');
      }
      return true;
    }
于 2012-08-08T13:10:56.847 に答える