首页 > 其他 > 详细

事件代理on

时间:2018-11-15 22:02:02      阅读:139      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>on</title>
</head>
<body>
<input type="text"><input type="button" value="添加">
<ul>

</ul>
<script src="js/jQuery3.3.1.js"> </script>
<script>

    $(function () {
        $("ul").on("click", "li", function () {  //事件代理
           console.log($(this).text())    //记住$ 取当前值
        });

        $("input[type=button]").click(function () {
            let txt = $("input[type=text]").val();    //input取值用val
            $(`<li>${txt}</li>`).appendTo("ul");
            })
        
         //未来追加的元素  是没有事件 我们通过事件委托  当你出现点击页面中的DOM没有任何反应
        //1.DOM是否underfine  2.考虑事件代理
    })
</script>
</body>
</html>

 

事件代理on

原文:https://www.cnblogs.com/tcpblog/p/9965968.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!