jquery匹配元素替换目标元素
DOM树结构如下:
<div class="container"> <div class="inner first">Hello</div> <div class="inner second">And</div> <div class="inner third">Goodbye</div> </div>
replaceAll(target)删除与节点相关联的所有数据和事件处理程序
用来替换的元素从老地方移到新位置,而不是复制:
$('.first').replaceAll('.third');DOM树变为:
<div class="container"> <div class="inner second">And</div> <div class="inner first">Hello</div> </div>
replaceWith(newContent)从DOM中移除内容,然后在这个地方插入新的内容
$('div.second').replaceWith('<h2>New heading</h2>');DOM树变为:
<div class="container"> <div class="inner first">Hello</div> <h2>New heading</h2> <div class="inner third">Goodbye</div> </div>
replaceAll()和.replaceWith()功能类似,但是目标和源相反:$('.first').replaceAll('.third');等价于$('div.third').replaceWith($('.first'));
注意:jQuery1.9之前replaceWith()返回一个新的jQuery集合,
jQuery1.9开始replaceWith()返回原始未修改的集合
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17