码迷,mamicode.com
首页 > 其他好文 > 详细

事件对象event和计时器

时间:2016-06-25 21:46:15      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

事件对象:event
    属性:
        srcElement事件源对象
        keyCode 键盘按键Ascii码

window方法:
    定时器:
    1)setTimeout();//n毫秒后执行一次
    2)setInterval();//每隔n秒执行一次
    这两个方法都有个返回值,返回一个定时器id,可以定义一个变量接收
    清除定时器方法:
    setTimeout()对应的是 clearTimeout(id);
    setInterval()对应的是 clearInterval(id);
技术分享
<html>
    <head>
    
    </head>
    
    <body>
        <div onclick="show1()">aaaa</div>
        <h2 onclick="show1()">bbb</h2>
        <p onclick="show1()">www</p>
    </body>
    
    <script>
        //弹出对应的内容
        function show(obj){
            alert(obj.innerText);
        }
        //我不希望使用this关键字,
        function show1(){
            alert(event.srcElement.innerText);
        }
    </script>
</html>
View Code
技术分享
<html>
    <head>
    
    </head>
    
    <!--<body onkeypress="show()">-->
    <body onkeyup="show()">
        <input type="text" onkeyup="if(this.value!=this.value.toUpperCase())this.value=this.value.toUpperCase()"/>
    </body>
    <script>
        function show(){
            alert(event.keyCode);
            if(event.keyCode=="27"){
                window.close();
            }
        }
    </script>
</html>
View Code
技术分享
<html>
    <head>
    <!--定时器Interval-->
    </head>
    <body>
        <div id="one" style="color:red;font-size:10cm;text-align:center">
        0
        </div>
        <input type="button" onclick="stop1()" value="stop"/>
        <input type="button" onclick="start1()" value="start1"/>
    </body>
    <script>
        var one=document.getElementById("one");
        var i=1;
        var dt="";
        function start1(){
            dt=setInterval(function(){
                one.innerText=i;
                i++;
            },"100");
        }
        
        function stop1(){
            clearInterval(dt);
        }
    </script>
</html>
定时器
技术分享
<html>
    <head>
    <!-- 不做了,思路:
        按enter键停止,将xs,ys替换为0,再次按,判断xs和ys是否为0,是的话,讲根据fx给xsys赋值。
        实现鼠标点哪往哪里走:获取鼠标的坐标,和现在的左边,确定应该往x走多少,y走多少,根据x y的值调走的速度
    -->
    </head>
    <body onkeydown="opt()">
        <img border="0" id="ren" src="images/q_1.jpg" style="position:absolute;left:0px;top:0px;">
    </body>
    <script>
        var ren=document.getElementById("ren");
        var fx="q";
        function transStr(obj){
            //return obj.substring(obj.lastIndexOf("/")+1,obj.length);
            return obj.substr(obj.lastIndexOf("/")+1);
        }
        function changetu(){
            if(transStr(ren.src).charAt(2)=="1")
                ren.src="images/"+fx+"_2.jpg";
            else
                ren.src="images/"+fx+"_1.jpg";
        }
        
        
        function start1(){
            setInterval(function(){
                run();
                changetu();
            },400);
        }
        start1();
        function opt(){
            var code=event.keyCode;
            switch(code){
            case 37://
                fx="z"
                ys=0;
                xs=-5;
                break;
            case 39://
                fx="y";
                ys=0;
                xs=5;
                break;
            case 38://
                fx="h";
                xs=0;
                ys=-5;
                break;
            case 40://
                fx="q";
                xs=0;
                ys=5;
                break;
            }
        }
        var x=0;
        var y=0;
        var xs=0;
        var ys=0;
        function run(){
            x+=xs;
            y+=ys
            ren.style.left=x;
            ren.style.top=y;
        }
    </script>
</html>
走路游戏

图自己找

事件对象event和计时器

标签:

原文地址:http://www.cnblogs.com/aigeileshei/p/5616935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!