[js]js中事件的3要素

js中事件的3要素

  • 事件源
  • 事件
  • 事件处理程序

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        #demo {
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>
<body>
<div id="demo"></div>
<button id="btn">改变宽度</button>
<script>
    /*要操作先找人*/
    var demo = document.getElementById("demo");  //获得id为demo的div盒子给demo
    var btn = document.getElementById("btn");    // 获得按钮
    /*事件三要素*/
    /*事件源.事件 = fucntion(){}*/
    btn.onclick = function(){
        console.log(this); // this表示btn
        demo.style.width = "400px";
    }
</script>
</body>
</html>
posted @ 2018-02-05 14:21  mmaotai  阅读(585)  评论(0编辑  收藏  举报