5秒后删除新的div

时间:2021-05-23 15:31:05

i want to add a div and after 5 seconds remove it. i tried

我想添加一个div,并在5秒后删除它。我试过了

$("#mainContentTD").prepend("<div style='color: #038a00; padding-bottom:10px;'>Your detailes where sucsesfuly... </div>").delay(5000).remove("this:first-child");

2 个解决方案

#1


29  

You can use setTimeout like this:

您可以像这样使用setTimeout:

setTimeout(function(){
  $('#divID').remove();
}, 5000);

The 5000 (ms) means 5 seconds. You should replace divID with your own div/element id.

5000(ms)表示5秒。您应该用您自己的div / element id替换divID。

You can make sure that the div exists first using length:

您可以使用length确保div首先存在:

setTimeout(function(){
  if ($('#divID').length > 0) {
    $('#divID').remove();
  }
}, 5000)

#2


8  

The .delay() method only works with methods that utilize the standard effect queue or a custom queue.

.delay()方法仅适用于使用标准效果队列或自定义队列的方法。

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

.delay()方法最适合延迟排队的jQuery效果。因为它是有限的 - 例如,它没有提供取消延迟的方法 - .delay()不能替代JavaScript的本机setTimeout函数,这可能更适合某些用例。

I.e., you could either use setTimeout(): (Demo)

即,您可以使用setTimeout():(演示)

var $elm = $("<div style='color: #038a00; padding-bottom:10px;'>Your detailes where sucsesfuly... </div>");
$("#mainContentTD").prepend($elm);
setTimeout(function() {
    $elm.remove();
}, 5000);

Or, you could use a effect method to remove to element: (Demo)

或者,您可以使用效果方法删除元素:(演示)

$("#mainContentTD")
    .prepend("<div style='color: #038a00; padding-bottom:10px;'>Your detailes where sucsesfuly... </div>")
    .children(':first')
    .delay(5000)
    .fadeOut(100);

#1


29  

You can use setTimeout like this:

您可以像这样使用setTimeout:

setTimeout(function(){
  $('#divID').remove();
}, 5000);

The 5000 (ms) means 5 seconds. You should replace divID with your own div/element id.

5000(ms)表示5秒。您应该用您自己的div / element id替换divID。

You can make sure that the div exists first using length:

您可以使用length确保div首先存在:

setTimeout(function(){
  if ($('#divID').length > 0) {
    $('#divID').remove();
  }
}, 5000)

#2


8  

The .delay() method only works with methods that utilize the standard effect queue or a custom queue.

.delay()方法仅适用于使用标准效果队列或自定义队列的方法。

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

.delay()方法最适合延迟排队的jQuery效果。因为它是有限的 - 例如,它没有提供取消延迟的方法 - .delay()不能替代JavaScript的本机setTimeout函数,这可能更适合某些用例。

I.e., you could either use setTimeout(): (Demo)

即,您可以使用setTimeout():(演示)

var $elm = $("<div style='color: #038a00; padding-bottom:10px;'>Your detailes where sucsesfuly... </div>");
$("#mainContentTD").prepend($elm);
setTimeout(function() {
    $elm.remove();
}, 5000);

Or, you could use a effect method to remove to element: (Demo)

或者,您可以使用效果方法删除元素:(演示)

$("#mainContentTD")
    .prepend("<div style='color: #038a00; padding-bottom:10px;'>Your detailes where sucsesfuly... </div>")
    .children(':first')
    .delay(5000)
    .fadeOut(100);