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

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

    You said it yourself --- there's not much difference. My choice of what to use, however, normally depends on method chaining, provided you already have your elements referenced.

    i.e.

    var _target, _content;
    
    _target.append(_content).addClass('foo');
    // will add the foo class to _target
    
    _content.appendTo(_target).addClass('foo');
    // will add the foo class to _content
    

提交回复
热议问题