ITeye博客

jquery匹配元素替换目标元素

小丑鱼0703 · 2015-03-04 10:47 · jquery · Web前端 · Markdown

DOM树结构如下: [code="html"]

Hello

And

Goodbye

[/code] [b]replaceAll(target)[/b] 删除与节点相关联的所有数据和事件处理程序 用来替换的元素从老地方移到新位置,而不是复制: [code="Jquery"]$('.first').replaceAll('.third');[/code]DOM树变为: [code="html"]

And

Hello

[/code] [b]replaceWith(newContent)[/b] 从DOM中移除内容,然后在这个地方插入新的内容 [code="Jquery"]$('div.second').replaceWith('

New heading

');[/code]DOM树变为: [code="html"]

Hello

New heading

Goodbye

[/code] replaceAll()和.replaceWith()功能类似,但是目标和源相反:$('.first').replaceAll('.third');等价于$('div.third').replaceWith($('.first')); 注意:jQuery1.9之前replaceWith()返回一个新的jQuery集合, jQuery1.9开始replaceWith()返回原始未修改的集合

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。