javascript - remove element in jquery then append a new element? - Stack Overflow

I am trying to remove an span element from HTML then replace that element with a new span element.View

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
Add a ment  | 

3 Answers 3

Reset to default 4

Use .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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信