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

前端 未结 7 720
长情又很酷
长情又很酷 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:17

    Like they say, mostly append() and appendTo produce the same results. However, when you start chaining the result, you can see a difference!

    Target HTML:

    First with append():

    $( ".inner1" )
      .append   ( "

    Test

    " ) .addClass ( "addBorder" );

    Produces:

    Test

    Second with appendTo()

    $( "

    Test

    " ) .appendTo ( ".inner2" ) .addClass ( "addBorder" );

    Produces:

    Test

提交回复
热议问题