【发布时间】:2011-02-15 11:52:07
【问题描述】:
哦,天哪,今天来了很多 - 哎呀
伙计们,最好的方法是:
$j('.done').append('Your services have been updated');
(完成的部分)
然后在说 5 秒后删除附加,这样如果一个人重新提交表单(允许),附加不会继续添加文本?即更新一次“您的服务已更新”,两次将显示“您的服务已更新您的服务已更新”,但我只希望您的服务已更新显示一次
【问题讨论】:
哦,天哪,今天来了很多 - 哎呀
伙计们,最好的方法是:
$j('.done').append('Your services have been updated');
(完成的部分)
然后在说 5 秒后删除附加,这样如果一个人重新提交表单(允许),附加不会继续添加文本?即更新一次“您的服务已更新”,两次将显示“您的服务已更新您的服务已更新”,但我只希望您的服务已更新显示一次
【问题讨论】:
(function (el) {
setTimeout(function () {
el.children().remove('span');
}, 5000);
}($j('.done').append('<span>Your services have been updated</span>')));
我知道这看起来很奇怪。我这样做是为了自我控制。调用该匿名函数时会立即完成追加...然后将该附加元素保存为该匿名范围内的el,当timeout 在5000 毫秒后触发时,该元素本身会被删除。
编辑:
我编辑了上面的函数,所以它不会破坏父元素(很抱歉)
【讨论】:
通过增加或减少delay(#)来设置你的时间
$j('.done').delay(2000).fadeOut(200,function() {
$(this).html('Your services have been updated').fadeIn(200);
});
【讨论】:
另一种解决方案:) 什么代码:
在 4.5 秒后新的 span 淡出并自行移除。
jQuery('<span />',{ style:'display:none' })
.html('Mesaj trimis...')
.appendTo(jQuery('.add-comment'))
.fadeIn('slow',
function(){
var el = jQuery(this);
setTimeout(function(){
el.fadeOut('slow',
function(){
jQuery(this).remove();
});
}, 4500);
});
【讨论】:
我知道这有点晚了,但我想与您分享我的解决方案,因为我将它添加到我的全局功能中。
function alert(elem,content,type,posi){
//elem - element to be inserted
//content - content of the alert box (based from bootstrap)
//type - alert type
//posi - either append or prepend
//primary, secondary, success, warning, info, danger, light, dark
if(type == undefined)
type = 'info';
elem.find('.glb-alert').remove();
var a = '<div class="alert alert-danger col-md-12 glb-alert" role="alert" >'+content+'</div>';
if(posi == undefined|| posi == 'prepend')
elem.prepend(a).find('.glb-alert').delay(1200).fadeOut();
else if(posi == 'append')
elem.append(a).find('.glb-alert').delay(1200).fadeOut();
}
【讨论】:
我不确定这里的上下文,但它会起作用吗
<span id="special_message">Your services have been updated.</span>`
然后删除那个特定的 ID 标记?
【讨论】:
您可能会考虑 Ben Alman 的 doTimeout 插件 - http://benalman.com/projects/jquery-dotimeout-plugin/
$j('<span>Your services have been updated</span>')
.appendTo('.done')
.doTimeout( 5000, 'remove' );
【讨论】:
// a little jQuery plugin...
(function($){
$.fn.timeout = function(ms, callback)
{
var self = this;
setTimeout(function(){ callback.call(self); }, ms);
return this;
}
})(jQuery);
// ...and how to use it.
$("<span>Your services have been updated</span>")
.appendTo('.done')
.timeout(5000, function(){ this.remove(); });
【讨论】:
我喜欢这种在删除跨度之前使用短暂淡出的方法。它还会在附加之前删除以前的消息。
$("<span class='js_msg'>Your services have been updated</span>")
.appendTo($j('.done').children("span.js_msg").remove().end())
.each(function(){
var self = $(this);
setTimeout(function () {
self.fadeOut(500,function(){
self.remove();
});
}, 4500);
}
);
【讨论】: