js中事件的3要素

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

[js]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 ></div>
<button >改变宽度</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>

相关文章:

  • 2021-12-14
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-04-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-09
  • 2021-07-26
  • 2022-02-04
  • 2022-12-23
  • 2021-07-20
  • 2021-05-13
相关资源
相似解决方案