append()
1.create content
- <h2>Greetings</h2>
- <div class="container">
- <div class="inner">Hello</div>
- <div class="inner">Goodbye</div>
- </div>
<h2>Greetings</h2>
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>
2.select an element on the page and insert it into another:(注意是move,而不是cloned)
- $('.container').append($('h2'));
-
$('.container').append($('h2'));
//<h2>Greetings</h2>被移动到下方去了
3.additional arguments--with functions
$(selector).append(function(index,html))
index - 可选。接收选择器的 index 位置。
html - 可选。接收选择器的当前 HTML。
appendTo()
$(content).appendTo(selector)
4.比较:
append() 和 appendTo() 方法执行的任务相同。不同之处在于:内容和选择器的位置,以及 append() 能够使用函数来附加内容