admin管理员组文章数量:1391951
I am trying to remove an span element from HTML then replace that element with a new span element.
View the fiddle for a working example: /
<div id="foo">
<span>FadeOut</span>
</div>
<input id="go" type="button" value="lets go!" />
$('#go').click(function() {
$('#foo span').fadeOut(500, function() {
$(this).remove().parent().append('<span>FadeIn</span>').fadeIn(500);
});
});
As always I am grateful for your help stack!
I am trying to remove an span element from HTML then replace that element with a new span element.
View the fiddle for a working example: http://jsfiddle/DCJ9X/
<div id="foo">
<span>FadeOut</span>
</div>
<input id="go" type="button" value="lets go!" />
$('#go').click(function() {
$('#foo span').fadeOut(500, function() {
$(this).remove().parent().append('<span>FadeIn</span>').fadeIn(500);
});
});
As always I am grateful for your help stack!
Share Improve this question asked Aug 28, 2011 at 10:05 XavierXavier 8,34218 gold badges56 silver badges85 bronze badges 1- 1 you can use the replaceWith method. :) api.jquery./replaceWith – Vince V. Commented Aug 28, 2011 at 10:10
3 Answers
Reset to default 4Use .replaceWith()
instead:
var $span = $('<span>FadeIn</span>');
$(this).replaceWith($span);
$span.fadeIn(500);
http://jsfiddle/DCJ9X/4/
Or on one line:
$(this).replaceWidth($('<span>FadeIn</span>').fadeIn(500));
This should do the trick:
$('#foo span').fadeOut(500).replaceWith('<span>FadeIn</span>').hide().fadein(500);
Well, if'n you wanted to keep more or less what you have, this works:
$('#go').click(function() {
var $foo = $('#foo');
$foo.find('span').fadeOut(500, function() {
$foo.remove('span').append('<span>FadeIn</span>').fadeIn(500);
});
});
本文标签: javascriptremove element in jquery then append a new elementStack Overflow
版权声明:本文标题:javascript - remove element in jquery then append a new element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744735652a2622311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论