个性化阅读
专注于IT技术分析

jQuery unwrap()

本文概述

jQuery unwrap()方法用于删除所选元素的父元素。

句法:

$(selector).unwrap()

jQuery unwrap()方法的示例

让我们以一个示例来演示jQuery unwrap()方法。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.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>Hello Guys!</p>
</div>
<article>
<p>This is srcmini02.com</p>
</article>
<button>Click here to remove the parent element of each p element</button>
</body>
</html>

立即测试

jQuery unwrap()示例2

让我们来看一个示例, 其中一起显示wrap()和unwrap()方法。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.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>Hello Guys!</p>
<p>This is srcmini02.com</p>
<button id="btn1">Wrap a div element around each p element</button>
<button id="btn2">Unwrap</button>
</body>
</html>

立即测试

赞(0)
未经允许不得转载:srcmini » jQuery unwrap()

评论 抢沙发

评论前必须登录!