jQuery unwrap() 方法用于移除被选元素的父元素,即外部包裹的元素。

语法

$(selector).unwrap() 

例子

例1

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").unwrap();
    });
});
</script>
<style>
div{background-color: orange;}
article{background-color: yellowgreen;}
</style>
</head>
<body>
<div>
<p>jQuery unwrap() 方法例子</p>
</div>
<article>
<p>这是p标签的内容</p>
</article>
<button>点击这里删除每个p元素的父元素</button>
</body>
</html>
例2

 <!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function(){
    $("#btn1").click(function(){
        $("p").wrap("<div></div>");
    });
    $("#btn2").click(function(){
        $("p").unwrap();
    });
});
</script>
<style>
div{background-color: pink;}
</style>
</head>
<body>
<p>jQuery unwrap() 方法例子</p>
<p>这是p标签的内容</p>
<button id="btn1">wrap() 方法</button>
<button id="btn2">unwrap() 方法</button>
</body>

</html>
效果如下所示:

jQuery unwrap() 方法