clone克隆文本框和remove

<body>
    <div class="outer">
        <div class="item">
            <button onclick="add(this)">+</button>
            <input type="text">
        </div>
    </div>

    <script src="jquery-3.3.1.min.js"></script><!--不加此句$不识别-->
    <script>
        function add(self) {
            //var clone=$(".item").clone();//克隆就是复制一份
            var clone=$(self).parent().clone();//this获取当前标签位置,self是形参对象

            clone.children("button").html("-").attr("onclick","remove(this)");//网页可以查看到添加了一个新的button
            //button事件onclick=remove(this)
            //html内容是-


            $(".outer").append(clone)
        }
        function remove(self) {
            $(self).parent().remove();
        }
    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/wfl9310/p/9209976.html
今日推荐