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

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

    It's mostly a matter of taste.

    One situation where appendTo is more convenient is when you have a reference to an element rather than a jQuery object, for example in the variable dest:

    $("
    ").appendTo(dest);

    To use the append method you have to create a jQuery object for the element to be able to call it:

    $(dest).append($("
    "));

提交回复
热议问题