jQuery wrapAll()的应用实例

jQuery wrapAll()的应用实例

wrapAll()方法是jQuery内置的一个方法,当指定的元素将被包裹在所有选择的元素中时使用。

语法:

$(selector).wrapAll(wrap_element)

参数:该方法接受一个参数wrap_element,这是必须的。这个参数用于指定哪个元素被包裹在所选元素周围。

返回值:该方法返回选定的元素和wrapAll()方法所做的指定修改。

下面的例子说明了jQuery中的wrapAll()方法。

示例:

<!DOCTYPE html>
<html>
    <head>
        <title>The wrapAll Method</title>
        <script src=
        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            (document).ready(function() {
                ("button").click(function() {
                    $("p").wrapAll("<div></div>");
                });
            });
        </script>
        <style>
            div {
                background-color: lightgreen;
                padding: 20px;
                width: 200px;
                font-weight: bold;
                height: 60px;
                border: 2px solid green;
            }
        </style>
    </head>
    <body>
        <!-- click on this paragraph and see the change -->
        <p>Welcome to GeeksforGeeks!<br><br>
          
        <button>Click Here!</button></p>
    </body>
</html>

输出:
jQuery wrapAll()的应用实例

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程