用闭包实现事件传参

2017-04-21 19:00 出处:360java.com 作者:360java  阅读()
!DOCTYPE htmlhtmlhead lang="en" meta charset="UTF-8" title/title style .box { width: 100px; height: 100px; background - color: goldenrod; position: absolute; left: 0 ; } /style/headbody button id="btn1"按钮1/button button id="btn2"按钮
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: goldenrod;
            position: absolute;
            left: 0;
        }
    </style>
</head>
<body>
    <button id="btn1">按钮1</button>
    <button id="btn2">按钮2</button>
    <div class="box" id="box"></div>
</body>
</html>
<script>
    var btn1=document.getElementById("btn1");
    var btn2=document.getElementById("btn2");
    var box=document.getElementById("box");
    function move(speed) {
        return function () {
            box.style.left=box.offsetLeft+speed+"px";
        }
    }
    btn1.onclick=move(5);
    btn2.onclick=move(-5);
</script>

 

分享到:
本文标签: 用闭包, 实现, 事件, 传参

相关文章

发表评论(共条评论)愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

Copyright (C) java学习 360java 360java.com, All Rights Reserved.

苏ICP备16022210号