使用JQuery 5秒后隐藏元素的实例

问题描述:

我试图实现通知样式JQuery函数。这里是我到目前为止使用JQuery 5秒后隐藏元素的实例

function notify(message, type, autohide, delay) { 
    message = message; 
    type = type || ""; 
    autohide = autohide || false; 
    delay = delay || 0; 
    $('#notification-container').append("<div class='notice " + type + "'>" + message + "<div class='close-container'><span class='glyphicon glyphicon-remove'></span></div></div>"); 
}; 

调用此函数正确添加了通知,但我一直没能做出后的时间“延迟”时期,特定元素被删除,不删除其他通知。我搜索过,但只找到#id的解决方案或基于类。我不想在每个新通知中都加上id,如果我通过.notice删除它,所有通知都将同时过期。我已经得到最近的一直使用

if (autohide) { 
    setTimeout(function() { 
    $('#notification-container .notice:last-child').remove(); 
    }, delay); 
} 

但我敢肯定,你们都可以看到这是如何有缺陷。任何帮助或建议,将不胜感激。

+0

我看不出有什么错在你的代码。你在控制台中看到任何错误吗?如果您的主代码中有可用的声明,那么在哪里? –

+0

http://*.com/questions/3655627/jquery-append-object-remove-it-with-delay –

+0

@ÁlvaroTouzón我已经看到了这个问题,但它基于类去除它。 – WiseOlMan

可以使元素的jQuery对象,并使用该引用删除它

function notify(message, type, autohide, delay) { 
    message = message; 
    type = type || ""; 
    autohide = autohide || false; 
    delay = delay || 0; 
    var $notification = $("<div class='notice " + type + "'>" + message + "<div class='close-container'><span class='glyphicon glyphicon-remove'></span></div></div>"); 
    $('#notification-container').append($notification); 


    if (autohide) { 
    setTimeout(function() { 
     $notification.remove();  
    }, delay); 
    } 
} 
+0

哇,我真的这个答案,虽然我不知道它是如何工作的。当你说$ notification.remove()时,jquery如何知道我正在谈论哪个$通知? – WiseOlMan

+0

而不是附加一个匿名html字符串,该字符串被转换为dom元素..你正在附加一个由jQuery对象表示的dom元素。对象的引用在追加过程中不会丢失 – charlietfl

+0

至于知道哪个...函数调用的每个实例中只有一个范围变量对象 – charlietfl

你可以尝试这样的事:

var deleteNotice = function(elem,delay){ 
    var tout = setTimeout(function(){ 
     clearTimeout(tout); 
     elem.remove() 
    },delay);//Now this acts on only this element 
} 

function notify(message, type) { 
    $('#notification-container').append("<div class='notice " + type + "'>" + message + "<div class='close-container'><span class='glyphicon glyphicon-remove'></span></div></div>"); 
    //Now assign this element to a variable, so everytime your function is called el represents the latest notice 
    var el = $('#notification-container .notice:last-child'); 
    deleteNotice(el,10000);//A function to delete this selected element 
}; 
+0

让我试试这个,但是不会遇到从#notification-container中删除最后一个元素的问题,它可能是之后添加的通知? – WiseOlMan

+0

范围界定将为您节省开支!您传递的最后一个孩子要删除通知是一个jQuery元素,而不是每个人说的“最后一个孩子”。 – Anubhab

+0

工程!做得太好了。只要它让我来选择答案。 – WiseOlMan

我会创造的元素,并将它们存储在本地变量,这种方式只适用于函数调用的元素将被删除。像

function notify(message, type, autohide, delay) { 
    var div = $('<div />', { 
     'class' : 'notice' + (type ? ' '+type : ''), 
     text : message || '' 
    }), 
     close = $('<div />', { 
      'class' : 'close-container', 
      on  : { 
       click : function() { 
        div.remove(); 
       } 
      }, 
      html : $('<span />', { 
       'class' : 'glyphicon glyphicon-remove' 
      }) 
    }); 

    if (autohide) { 
     div.delay(delay||0).hide(0, function() { 
      $(this).remove(); 
     }); 
    } 

    $('#notification-container').append(div.append(close)); 
} 
东西

FIDDLE

Here's a version that supports animations

+0

我从来没有见过你创建div的方式。你知道一个很好的资源,我可以在那里读到吗?甚至只是它的名字? – WiseOlMan

+0

@WiseOlMan - 当然,它在[** jQuery documentation **](http://api.jquery.com/jquery/#entry-examples-1)中,并且是创建元素的首选方式。 – adeneo