事件委托

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>attr-prop</title>

    <style>
       #box {
           width: 400px;
           background: green ;
       }

       .a {
           width: 200px;
           height: 200px;
           background: red;
       }

       .b {
           width: 100px;
           height: 100px;
           background: yellow;
       }
    </style>
 
</head>
<body>
 
    <div id="box">
        <div class="a">a的事件委托</div>
        <br>
        <br>
        <br>
        <div class="b">b的事件委托</div>
        <br>
        <br>
        <br>

    </div>

    <script src="../zepto.js"></script>
    <script src="../touch.js"></script>
    <script>
        $(function(){
 
            $('#box').on('touchstart','.a',function(){
               alert('a 触发的事件')
               console.log(this);
            })

            $('#box').on('touchstart','.b',function(){
                console.log(this)
               alert('b 触发的事件')
            })
 
            $('#box').append('<p class="a">我是新添加的</p>')

        })
    </script>

</body>
</html>
posted @ 2022-02-17 15:57  13522679763-任国强  阅读(19)  评论(0)    收藏  举报