jq-append和appendTo 的区别

来源:互联网 发布:sql删除重复的数据 编辑:程序博客网 时间:2024/06/05 00:37


在jQuery的文档操作方法中,append()和appentto()方法执行的任务相同,但是两者也有区别。

1、append()方法:在被选元素的结尾(但仍在元素内部)插入指定的内容。

语法:

a.$(selector).append(content);//参数content是必需的,指定要附加的内容。
//例:
<script>
$("p").append(" <b>Hello jQuery!</b>"); 
</script>
...
<body>
<p>This is another paragraph.======</p> 
</body>
...
//结果:
This is another paragraph.====== Hello jQuery!

b.$(selector).append(function(index,html)); //function()是必需的,参数index和html都是可选的。index表示接收选择器的index位置,html表示接收选择器的当前html。



2、appendto()方法:在被选元素的结尾(但仍在元素的内部)插入指定的内容。但不能使用函数来附加内容。

语法:

$(content).appendto(selector);
// 例:
<script>
$("<b> Hello jQuery!</b>").appendTo("p"); 
</script>

...
<body>
<p>This is a paragraph.++++++</p> 
</body>
...
结果:
This is a paragraph.++++++Hello jQuery!



原创粉丝点击