我有这个功能:
$('#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">' +
' ' +
'<a href="javascript:void(0)" class="removeNote"><i class="icon-remove"></i></a>' +
' ' +
'</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
并没有别的。
我该如何修复此代码?
由于
答案 0 :(得分:0)
替换您的代码:
newnote.remove();
用这个:
$(this).closest('#notenote').remove();
答案 1 :(得分:0)
您也可以直接更改noteContainer div的内容 像这样
$("#noteContainer").html("");
希望它有所帮助...