jQuery DOM operation unwrap() method

We can add a wrapped parent element to the selected element through the wrap method. On the contrary, what should be done if the parent element of the selected element is deleted?

jQuery provides an unwarp() method, which is opposite to the wrap method. Removes the parent element from the set of matching elements, leaving itself (and siblings, if present) in their original positions.

Let’s write an example below:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
    <div>
        <p>元素</p>
    </div>

    <script>
        $("p").unwrap("<div></div>");
    </script>
</body>
</html>

Friends can run it locally, and then press F12 in the browser to debug, or use the shortcut key ctrl+shift+i, you can See that the div tag we started writing has been deleted

Please note that we have used the remove or empty method to delete before. This will delete all the elements in it, while the unwrap method only deletes the parent node, but does not delete the child nodes. This method is relatively simple. You can refer to the above case

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> </head> <body> <div> <p>元素</p> </div> <script> $("p").unwrap("<div></div>"); </script> </body> </html>
submitReset Code