---
title: "jquery匹配元素替换目标元素"
author: "小丑鱼0703"
date: "2015-03-04T10:47:05Z"
tags: []
categories: ["Web前端"]
slug: "jquery"
draft: false
lastmod: "2015-03-04T10:47:05Z"
id: 2188638
url: "https://wangjingyi.iteye.com/blog/2188638"
---

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()返回原始未修改的集合