美文网首页
js-jQuery delegate

js-jQuery delegate

作者: AssertDo | 来源:发表于2019-08-21 10:30 被阅读0次
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        
        <input type="button" id="btn" value="增加一行" />
        <div id="dv">
            <p>11111</p>
            <p>22222</p>
            
        </div>
        
        <script src="js/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            
            $(function(){
                
                //新建p
                $("#btn").click(function(){
                    
                    $("<p>new p</p>").appendTo("div");
                });
                 
                //delegate:代理 , 委托
                //给父元素注册委托事件,让动态创建出来的p也能有事件
                $("div").delegate("p","click",function(){
                    alert("hehe");
                });
                
            });
            
        </script>
        
    </body>
</html>


相关文章

网友评论

      本文标题:js-jQuery delegate

      本文链接:https://www.haomeiwen.com/subject/xexbsctx.html