开发者社区 问答 正文

怎么在创建元素之前给他绑定事件

<!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 = '<div class="ibox"> ' +
                        '<input type="text" class="num"  maxlength="3" value=""/>' +
                        '<input type="button" value="删除"  class="del"/>' +
                        '</div>';
                $('#inputfield').append(tr);
            }
        })
    </script>
</head>
<body>


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




<div id="inputfield">
</div>


</body>
</html>

$('.del').on('click' ,function () {
$(this).parents('.ibox').remove();
})

展开
收起
小旋风柴进 2016-03-26 10:45:54 1854 分享 版权
1 条回答
写回答
取消 提交回答
  • `$('#inputfield').on('click' ,'.del',function () {
    $(this).parents('.ibox').remove();
    })`
    通过父元素来对其子元素绑定事件

    2019-07-17 19:15:42
    赞同 展开评论
问答地址: