jQuery DOM operation wrapAll()

Wrap is for a single DOM element. If you want to wrap the elements in the collection with other elements, that is, add a parent element to them. For such processing, JQuery provides a wrapAll method

wrapAll(wrappingElement): Add an outer wrapping HTML structure to the matching elements in the collection

Let’s write an example

Look at the following code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>wrapALL</title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
    <p>php 中文网</p>
    <p>php.cn</p>

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

The above code, You can debug locally and press F12 to view. We can see that two p tags are wrapped by a div tag

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>wrapALL</title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> </head> <body> <p>php 中文网</p> <p>php.cn</p> <script> $("p").wrapAll("<div></div>"); </script> </body> </html>
submitReset Code