日期:2014-05-16  浏览次数:20554 次

js的打字游戏的例子

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'c.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script type="text/javascript">
 var score = 0;
 //产生一个字母
  function getEnglishChar()
  {
   var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   var ch = str.charAt(Math.round(Math.random()*35));
   return ch;
  }
  //将字母写到屏幕上
  function display()
  {
   var ch = getEnglishChar();
   var lab = document.createElement("label");
   lab.innerHTML = ch;
   with(lab.style)
   {
    left = Math.round(document.body.clientWidth * Math.random());
    top = "0px";
    position = "absolute";
    fontSize = "20px";
    color = "green";
   }
   document.body.appendChild(lab);
  }
  //字母掉下来
  function  down()
  {
   var chs = document.getElementsByTagName("label");
   if(chs != null)
   {
    for(i = 0; i < chs.length; i++)
    {
     chs[i].style.top = parseInt(chs[i].style.top) + 20 +"px";
     if(chs[i].style.offsetHeight + chs[i].style.offsetTop >= document.body.clientHeight)
     {
      document.body.removeChild(chs[i]);
      score--;
     }
     alert(score);
    }
   }
  }
  
  var s1 = setInterval("display()",1000);
  var s2 = setInterval("down()",100);
  //alert("asdas");
  document.onkeyup = function(event)
  {
   var str = document.getElementsByTagName("label");
   for(i = 0; i < str.length; i++)
   {
    var ch = str[i].innerHTML;
    e = event?event:window.event;
    //alert(ch.charCodeAt(0)==e.keyCode);
    //alert();
    if(ch.charCodeAt(0) == e.keyCode)
    {
  &