In which case it is better to use the .append(), and which .appendTo()?

前端 未结 7 706
长情又很酷
长情又很酷 2021-01-12 04:48

There is no big difference between those functions except the syntax:

$(\'.target\').append(\'text\');
$(\'text\').appendTo(\'.target\');

A

7条回答
  •  有刺的猬
    2021-01-12 05:07

    It's useful for when you chain a few jQuery actions together. like so:

    $('.target').css({display:"block"}).fadeOut().appendTo('.somewhere');
    // appends target to somewhere
    

    or:

    $('.target').css({display:"block"}).fadeOut().append('.somewhere');
    // appends somewhere to target
    

    those two are different actions, hence it is useful to have them both.

提交回复
热议问题