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);
});
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744735652a4590729.html
评论列表(0条)