黑马程序员技术交流社区

标题: Javascript问题 [打印本页]

作者: wdj_student    时间: 2013-5-16 18:06
标题: Javascript问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<script type="text/javascript">
        var t1=t2=0;
        function start(){
                var words=document.getElementById("words");
                words.value="";
                //如果Cookie中不存在用户
                if(!Cookie("name")){
                        var _name=prompt("请输入你的用户名?");
                        Cookie("name",_name,{expires:360});
                        }
                var date=new Date();
                t1=date.getTime();//记录当前时间
                words.focus();  //获得焦点
                }
        function stop(){
                var words=document.getElementById("words");
                var date=new Date();
                t2=date.getTime();//记录暂停时的时间
                //计时打字用时
                var time=(t2-t1)/(1000*60);
                //记录输入的总字数
                var num=words.value.length;
                //计算打字速度
                rate=Math.round(num/time);
                //检测Cookie中是否存在历史成绩
                Cookie("rate")||Cookie("rate",0,{expires:360});
                if(parseInt(Cookie("rate"))<rate){
                        //如果现在成绩优于历史成绩,则存储该成绩
                                Cookie("rate",rate,{expires:360});
                        }
                //检测Cookie中的总字数
                var sum=Cookie("sum")?Cookie("sum"):0;
                //存储累计总字数
                Cookie("sum",(parseInt(sum)+num),{expires:360});
                var info="你输入的总字数:"+Cookie("sum")+"\n"+
                                        "本次打字数:"+num+"\n"+
                                        "本次打字速度(字/分):"+rate+"\n"+
                                        " 最好成绩:"+Cookie("rate")+"\n";
                words.value=info;
                }
        function clear(){
                Cookie("num",null);
                Cookie("sum",null);
                Cookie("rate",null);       
                var words=document.getElementById("words");
                words.value="";
        }
</script>
</head>

<body>
<input type="button"  id="start" value="开始测试打字速度" />
<input type="button"  id="stop" value="停止" />
<input type="button" id="clear" value="清除Cookie" /><br />
<textarea id="words" cols="80" rows="20"></textarea>
</body>
</html>


为什么没有结果?该怎么改

作者: SOAR    时间: 2013-5-16 18:25
你的按钮没有注册任何触发事件。
作者: wdj_student    时间: 2013-5-17 08:46
SOAR 发表于 2013-5-16 18:25
你的按钮没有注册任何触发事件。

就算注册了事件也不行
作者: 斩_梦    时间: 2013-5-18 00:44
<input type="button"  id="start" value="开始测试打字速度"  onclick="start()"/>,这样就可以啦,一般情况下JS不执行的情况下就是因为字母大小问题,都是细节问题,细节决定成败,应该不会有错的。
作者: wdj_student    时间: 2013-5-18 09:43
斩_梦 发表于 2013-5-18 00:44
,这样就可以啦,一般情况下JS不执行的情况下就是因为字母大小问题,都是细节问题,细节决定成败,应该不会 ...

不行啊,不信你可以试试




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2