---
title: "jquery-dom操作外部插入节点"
author: "小丑鱼0703"
date: "2015-01-15T14:27:55Z"
tags: []
categories: ["Web前端"]
slug: "jquery-dom"
draft: false
lastmod: "2015-01-15T14:34:56Z"
id: 2176198
url: "https://wangjingyi.iteye.com/blog/2176198"
---

外部插入是将content插入到selector节点外部，包括selector节点的前面和后面。插入后的节点与原节点是<span style="color:blue">**兄弟关系**</span>。

1.**after()** 选元素之后插入内容。
2.**insetAfter()** 与after对应。
3.**before()** 被选元素之前插入内容。
4.**insertBefore()** 与before对应。


```jquery

$("#btn1").click(function(){
    $("p").before("<b>Before</b>");
    //$("<b>Before</b>").insertBefore("p");
  });

  $("#btn2").click(function(){
    $("p").after("<i>After</i>");
    //$("<i>After</i>").insertAfter("p");
}

<p>段落1</p>
<button id="btn1">Insert before</button>
<button id="btn2">Insert after</button>

```


运行结果如下：
![](http://dl2.iteye.com/upload/attachment/0105/2982/0ae68214-2130-3857-b633-31e9fdfa26c8.jpg)