Javascript 如何在不刷新页面的情况下从DOM中删除prepend/append元素

Javascript 如何在不刷新页面的情况下从DOM中删除prepend/append元素,javascript,jquery,css,twitter-bootstrap,Javascript,Jquery,Css,Twitter Bootstrap,我正在使用bootstrap popover,我想向DOM添加某些元素。我在标题中添加了一个附加元素,使用: $('div.popover').prepend($('<h3>').html('Accounts Already exists').addClass('main-header-title')); $('.main-header-title').append('<span class="close" id="cross-button">&times;<

我正在使用bootstrap popover,我想向DOM添加某些元素。我在标题中添加了一个附加元素,使用:

$('div.popover').prepend($('<h3>').html('Accounts Already exists').addClass('main-header-title'));
$('.main-header-title').append('<span class="close" id="cross-button">&times;</span>');
$('div.popover').prepend($(''.html('Accounts ready exists').addClass('main-header-title'));
$('.main header title')。追加('×;');
但是,如果我不刷新页面,
('div.popover')
会一直在
元素前面加上前缀,我在popover中看到了重复的标题。类似于下面的屏幕截图:

有人能帮我把元素设置为null吗?这样当我再次搜索时(不刷新),它就不会显示重复的标题名


谢谢

只有在DOM中还没有它时,才可以使用

if ($('h3.main-header-title').length==0) {
     $('div.popover').prepend($('<h3>').html('Accounts Already exists').addClass('main-header-title'));
     $('.main-header-title').append('<span class="close" id="cross-button">&times;</span>');
}
为了获得更好的用户体验,如果您想重新显示popover,请这样做

if ($('h3.main-header-title').length==0) {
         $('div.popover').prepend($('<h3>').html('Accounts Already exists').addClass('main-header-title'));
         $('.main-header-title').append('<span class="close" id="cross-button">&times;</span>');
    }
else {
         $(h3.main-header-title).fadeOut(200);
         setTimeout(function(){$(h3.main-header-title).fadeIn(200);},500)
}
if($('h3.main header title')。长度==0){
$('div.popover').prepend($(''.html('Accounts ready exists').addClass('main-header-title'));
$('.main header title')。追加('×;');
}
否则{
$(h3.主标题)。淡出(200);
setTimeout(函数(){$(h3.main头标题).fadeIn(200);},500)
}

希望这有帮助

阅读更新后的答案,另一种方法是不要自动删除popover,当您想再次显示popover时,只需删除第一个popover,500毫秒后重新显示它,以获得更好的用户体验。如果它解决了您的问题,请升级投票并将其标记为正确,以便将来帮助他人:)
setTimeout(function(){
$("h3.main-header-title").remove();
// or $("h3.main-header-title").fadeOut(100); for animated removal
},1000);
if ($('h3.main-header-title').length==0) {
         $('div.popover').prepend($('<h3>').html('Accounts Already exists').addClass('main-header-title'));
         $('.main-header-title').append('<span class="close" id="cross-button">&times;</span>');
    }
else {
         $(h3.main-header-title).fadeOut(200);
         setTimeout(function(){$(h3.main-header-title).fadeIn(200);},500)
}