JQuery 淡入淡出按钮不起作用
我有一个带有保存按钮的表单。单击“保存”按钮时,它会运行 ajax 来保存表单内容。那行得通。我遇到的麻烦是保存按钮淡入淡出。保存按钮的文本是“另存为草稿”。单击时,我希望它淡出,将文本更改为“草稿保存于[时间]”,然后淡入以显示它已保存以及保存的时间。几秒钟后,我希望该按钮消息淡出并被原始的“另存为草稿”按钮取代。
我想我必须将淡入淡出嵌套到函数中,但我的 jquery 淡入淡出天赋很小,而且很快就消失了:)
下面是 ajax 的成功部分......
success: function(data) {
var date = new Date(),
hours = date.getHours(),
minutes = date.getMinutes();
if (hours > 12) hours = hours - 12;
if (minutes < 10) minutes = '0' + minutes;
var save_draft = '<div id="save-button" class="button-clone">Save as Draft</div>';
var draft_saved = '<div id="save-button" class="button-clone">Draft Saved at ' + hours + ':' + minutes + '</div>';
$('#save-button').fadeOut(1000);
$('#save-button').replaceWith(draft_saved);
$('#save-button').fadeIn(1000);
$('#save-button').fadeOut(1000);
$('#save-button').replaceWith(save_draft);
$('#save-button').fadeIn(1000);
}
I have a form which has a save button. When the save button is clicked it runs ajax to save the form contents. That works. What I am having troubles with is the save button fadein fadeout. The text of the save button is "Save as Draft". When clicked I would like it to fade out, change the text to "Draft saved at [time]" and then fade in to show that it was saved, and the time it was saved. After a few seconds I would like that button message to fade out and be replaced by the original "Save as Draft" button.
I'm thinking I have to nest the fades into functions, but my jquery fading talents are minimal and fading rapidly :)
Below is the success part of the ajax ....
success: function(data) {
var date = new Date(),
hours = date.getHours(),
minutes = date.getMinutes();
if (hours > 12) hours = hours - 12;
if (minutes < 10) minutes = '0' + minutes;
var save_draft = '<div id="save-button" class="button-clone">Save as Draft</div>';
var draft_saved = '<div id="save-button" class="button-clone">Draft Saved at ' + hours + ':' + minutes + '</div>';
$('#save-button').fadeOut(1000);
$('#save-button').replaceWith(draft_saved);
$('#save-button').fadeIn(1000);
$('#save-button').fadeOut(1000);
$('#save-button').replaceWith(save_draft);
$('#save-button').fadeIn(1000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于每个动画都是异步的,因此您需要调用回调函数内的任何其他方法。
编辑
抱歉,它不起作用。也用
fadeIn
进行调整:让我解释一下为什么需要这个
hide()
:当你
fadeOut
某个元素时,jQuery 会放置display : none
直接指向该 HTML。但是您正在替换为另一个 HTML(通过replaceWith()
方法)。但是fadeIn()
只会淡出隐藏的元素。因此,我们需要隐藏新的 HTML,然后fadeIn()
发生。Since each animation is async, you need to call any other method inside the callback function.
EDIT
Sorry, it wasn't working. Adjusted with
fadeIn
too:Let me explain why this
hide()
is being needed:When you
fadeOut
some element, jQuery putsdisplay: none
directly to that HTML. But you is replacing with another HTML (byreplaceWith()
method). ButfadeIn()
only fades elements that was hidden. So, we need to hide the new HTML and then thefadeIn()
happens.你说得对。你需要筑巢。尝试这样的事情......
You're right. You need to nest. Try something like this...
你应该这样做:
我没有缩进代码,所以它看起来不乱
希望这有帮助。干杯
You should do this:
I didn't indent the code so it doesn't look messy
Hope this helps. Cheers