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

前端 未结 7 709
长情又很酷
长情又很酷 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
    
    0 讨论(0)
提交回复
热议问题