jQuery删除但不是容器DIV

时间:2013-10-07 13:26:46

标签: jquery

我有这个功能:

$('#addNote').click(function() {

   var html =
            '<div class="row-fluid" id="notenote"><div class="span9">' +
            $('#noteContent').val() +
            '<input type="hidden" value="' + $('#noteContent').val() + '" name=notes[]>' +
            '</div>' +
            '<div class="span3 ">' +
            '<img width="68" height="60" src="getimage.php?image=current_admin">' +
            '<div class="pull-right">' +
            '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
            '<a href="javascript:void(0)" class="removeNote"><i class="icon-remove"></i></a>' +
            '&nbsp;&nbsp;' +
            '</div>' +
            '</div>' +
            '</div>';

    if ($('#noteContent').val() !== '') {
        $('#noteContent').val('');
        var newnote = $("#noteContainer").append(html);

        newnote.find('.removeNote').on("click", function(event) {
            event.preventDefault();
            newnote.remove();  
        });
    }
});

问题在于,当newnote.remove()被触发时,它也会删除主容器DIV #noteContainer,而它只应删除自身,所以DIV #notenote并没有别的。 我该如何修复此代码?

由于

2 个答案:

答案 0 :(得分:0)

替换您的代码:

newnote.remove();  

用这个:

$(this).closest('#notenote').remove();  

答案 1 :(得分:0)

您也可以直接更改noteContainer div的内容 像这样

$("#noteContainer").html("");

希望它有所帮助...