在这种情况下,最好是使用.append(),和其中.appendTo()? [英] In which case it is better to use the .append(), and which .appendTo()?

查看:96
本文介绍了在这种情况下,最好是使用.append(),和其中.appendTo()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有,除了语法这些功能之间没有大的区别:

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

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

说jQuery的文档

的.append()和.appendTo()方法执行相同的任务。专业
  差在语法 - 特别是在的安置
  内容和目标。与.append(),选择器的前pression preceding
  该方法是其中插入的内容的容器。同
  .appendTo(),在另一方面,内容precedes的方法中,
  无论是作为一个选择的前pression或动态创建标记,
  它被插入到目标容器

The .append() and .appendTo() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With .append(), the selector expression preceding the method is the container into which the content is inserted. With .appendTo(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.

所以,在这种情况下,最好是使用.append(),并.appendTo()?
其中code-样品只适合那些两个函数之一,另一个是不够好?

So in which case it is better to use the .append(), and which .appendTo()? In which code-samples fit only one of those two functions and the other is not good enough?

同样的问题适用于:

  • .prepend() vs .prependTo()
  • .before() vs .insertBefore()
  • .after() vs .insertAfter().

推荐答案

您自己说的---没有太多的区别。我对用什么选择,但是,通常依赖于方法链,前提是你已经有你的元素引用。

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.

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

这篇关于在这种情况下,最好是使用.append(),和其中.appendTo()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆