1

是否可以在按下(按下)时检测到我们的鼠标,并从零开始计数几秒钟......并且在鼠标按下 5 秒后,它会重定向。

我找到了一个脚本,但是我对如何在按下鼠标时显示第二个感到困惑。

这里是。

<script type="text/javascript">
/*<![CDATA[*/
document.getElementsByClassName=function(classname){
  var pattern=new RegExp("(^|\s)"+classname+"(\s|$)");
  var results=new Array();
  var d=document.getElementsByTagName('*'), l=d.length;
  for (var k=0; k<l; k++) if (pattern.test(d[k].className)) results.push(d[k]);
  return results;
}

var timer;

function startStuff(el,ii)
{
  var temp=el.innerHTML;
  timer=window.setTimeout('doStuff("'+temp+'");',3000);
}

function stopStuff(el)
{
  window.clearTimeout(timer);
}

function doStuff(l)
{
<?php echo"<html>"; header( 'Location: http://www.google.com' ) ; echo"</html>";?>
}

window.onload=function() {
  var links=document.getElementsByClassName('clicker');
  for (var i=0; i<links.length; i++)
  {
    links[i].onmousedown=function() {startStuff(this)};
    links[i].onmouseup  =function() {stopStuff(this)};
  }
}

/*]]>*/
</script> 

</head>

<body>
<a href="#" class="clicker">Link1</a>
<a href="#">Link2</a>
<a href="#" class="clicker">Link3</a>
</body>
</html>

如果可能的话,我希望第二个显示图像......

4

1 回答 1

1

我不会使用图像。尝试这样的事情:

link = document.getElementById('redirect');
counter = document.getElementById('counter');
timeRemaining = 5000;

link.onclick = function(e) {
  e.preventDefault();

  setInterval(function(){
    if (timeRemaining > 0) {
      timeRemaining -= 1000;
      counter.innerHTML = timeRemaining/1000;
    } else {
      window.location = link.href;
    }
  },1000);
};

完整的工作示例:

<!DOCTYPE html>
<html>
<head>
  <meta charset=utf-8 />
  <title>Redirect to Google</title>
</head>
<body>
  <a href="http://www.google.com" id="redirect">
    Go to google in <span id="counter">5</span> seconds
  </a>
  <script type="text/javascript">
    link = document.getElementById('redirect');
    counter = document.getElementById('counter');
    timeRemaining = 5000;

    link.onclick = function(e) {
      e.preventDefault();

      setInterval(function(){
        if (timeRemaining > 0) {
          timeRemaining -= 1000;
          counter.innerHTML = timeRemaining/1000;
        } else {
          window.location = link.href;
        }
      },1000);
    };
  </script>
</body>
</html>
于 2012-07-08T16:36:04.590 回答