码迷,mamicode.com
首页 > 其他好文 > 详细

事件代理on

时间:2018-11-15 22:24:19      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:round   事件   doc   lang   添加   jquer   span   div   html   

<!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

标签:round   事件   doc   lang   添加   jquer   span   div   html   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!