javascript - 怎么在创建元素之前给他绑定事件
阿神
阿神 2017-04-10 15:25:11
0
6
186
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>设置</title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script charset="utf-8">
        $(function () {
            $('.del').on('click' ,function () {
                $(this).parents('.ibox').remove();
            })
            var array = ["9", "2", "3", "4", "5"];
            $("#addnum").click(function () {
                for (i = 0; i < array.length; i++) {
                    add()
                    $(".num :eq(" + i + ")").val(array[i]);
                }
            })
            function add(){
                var tr = '<p class="ibox"> ' +
                        '<input type="text" class="num"  maxlength="3" value=""/>' +
                        '<input type="button" value="删除"  class="del"/>' +
                        '</p>';
                $('#inputfield').append(tr);
            }
        })
    </script>
</head>
<body>


<p>
    <button height="8" type="button" id="addnum">+</button>
</p>




<p id="inputfield">
</p>


</body>
</html>

"如上代码 尝试这创建了一批输入框 并使用on函数提前绑定元素 但是发现 如果把

$('.del').on('click' ,function () {
$(this).parents('.ibox').remove();
})
"
放到function add之内 添加元素del才有效果 放在add之外就不能附带效果,和文档里面说的“即使是执行on()函数之后新添加的元素,只要它符合条件,绑定的事件处理函数也对其有效。”不一致求解惑

阿神
阿神

闭关修行中......

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!