您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页jquery中append、prepend,before和after方法的区别

jquery中append、prepend,before和after方法的区别

来源:五一七教育网



1. append()和prepend()

假设

<p class='a'> //<---you want p c to append in this
 <p class='b'>b</p>
</p>

使用

$('.a').append($('.c'));

效果如下:

<p class='a'> //<---you want p c to append in this
 <p class='b'>b</p>
 <p class='c'>c</p>
</p>

同样使用

$('.a').prepend($('.c'));

效果如下:

<p class='a'> //<---you want p c to append in this
 <p class='c'>c</p>
 <p class='b'>b</p>

</p>

2. 使用after()和Before.html" target="_blank">before()

同样使用假设代码:

$('.a').after($('.c'));

效果如下:

<p class='a'>
 <p class='b'>b</p></p><p class='c'>c</p>

同样使用before()

$('.a').before($('.c'));

效果如下:

<p class='c'>c</p><p class='a'>
 <p class='b'>b</p></p>

总结:

append() & prepend()是在元素内插入内容(该内容变成该元素的子元素或节点),after() & before()是在元素的外面插入内容(其内容变成元素的兄弟节点)。

1. append()和prepend()

假设

<p class='a'> //<---you want p c to append in this
 <p class='b'>b</p>
</p>

使用

$('.a').append($('.c'));

效果如下:

<p class='a'> //<---you want p c to append in this
 <p class='b'>b</p>
 <p class='c'>c</p>
</p>

同样使用

$('.a').prepend($('.c'));

效果如下:

<p class='a'> //<---you want p c to append in this
 <p class='c'>c</p>
 <p class='b'>b</p>

</p>

2. 使用after()和before()

同样使用假设代码:

$('.a').after($('.c'));

效果如下:

<p class='a'>
 <p class='b'>b</p></p><p class='c'>c</p>

同样使用before()

$('.a').before($('.c'));

效果如下:

<p class='c'>c</p><p class='a'>
 <p class='b'>b</p></p>

总结:

append() & prepend()是在元素内插入内容(该内容变成该元素的子元素或节点),after() & before()是在元素的外面插入内容(其内容变成元素的兄弟节点)。

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务