区别就是:普通事件只支持单个事件,事件绑定可以添加多个事件。

代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>
<body>
    <button >button</button>
    <script type="text/javascript">
        var myBtn=document.getElementById("btn");

        myBtn.onclick=function  () {
            alert('普通事件1');//不执行
        }
        myBtn.onclick=function  () {
            alert('普通事件2');//弹出
        }

//      上面用普通方法添加两个事件,下面用事件绑定添加两个事件。

        myBtn.addEventListener('click',function  () {
            alert('事件绑定1');//弹出
        },false)
        myBtn.addEventListener('click',function  () {
            alert('事件绑定2');//弹出
        },false)

        //注意我这里只使用W3C的标准写法添加事件,没有兼容低版本的IE。

    </script>
</body>
</html>

 

相关文章:

  • 2022-12-23
  • 2021-08-16
  • 2021-07-24
  • 2018-04-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-11
猜你喜欢
  • 2021-12-31
  • 2021-10-03
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
相关资源
相似解决方案