删除以前的警报消息

时间:2014-08-07 13:38:58

标签: javascript jquery css twitter-bootstrap

我使用以下代码添加消息及其工作正常,问题是 当我把第二条消息放在preivos消息之上时,依此类推...... 当我添加新的

时,有一种方法可以从UI中删除previos消息
function addInfoMessage(message) {

    var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>&times;</button></div>");
    $messages.append($msg);

}

4 个答案:

答案 0 :(得分:4)

请勿附加,只需将内容替换为html()

$messages.html($msg);

答案 1 :(得分:2)

在jQuery中使用 empty() 删除所有子元素

 $messages.empty();
 var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>&times;</button></div>");
    $messages.append($msg);

答案 2 :(得分:2)

您必须选择该消息并将其删除,如下所示:

function addInfoMessage(message) {
    $messages.find('.alert.alert-info').remove();
    var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>&times;</button></div>");
    $messages.append($msg);
}

答案 3 :(得分:2)

您可以使用.html()代替.append(),这将替换$messages

中的所有html代码
function addInfoMessage(message) {

    var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>&times;</button></div>");
    $messages.html($msg);

}
相关问题