0

我有这个功能:

$('#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并没有别的。如何修复此代码?

谢谢

4

2 回答 2

0

替换您的代码:

newnote.remove();  

有了这个:

$(this).closest('#notenote').remove();  
于 2013-10-07T13:29:36.560 回答
0

您也可以像这样简单地更改 noteContainer div onclick 的内容

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

希望它有所帮助...

于 2013-10-07T13:36:37.303 回答