Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

jQuery wrapInner()

jQuery wrapInner()方法用于将HTML结构包装在匹配元素集中每个元素的内容周围。此方法可以接受可以传递给$()工厂函数的任何字符串或对象。
语法:
$(selector).wrapInner(wrappingElement,function(index))

jQuery wrapInner()方法的参数

参数 说明
wrappingElement 这是强制性参数。它指定了将哪些HTML元素包装在每个选定元素的内容周围。其可能的值为: HTML元素 jQuery对象 DOM元素
function(index) 这是一个可选参数。它指定一个返回包装元素的函数。 index:它提供元素在集合中的索引位置。

jQuery wrapInner()方法的示例

让我们以一个示例来演示jQuery wrapInner()方法。
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").wrapInner("<em></em>");
    });
});
</script>
</head>
<body>
<p>Hello Guys!</p>
<p>This is lidihuo.com</p>
<button>Wrap a emphasized element around the content of each p element</button>
</body>
</html>
输出:

Hello Guys!

This is lidihuo.com

注意:您还可以使用多个元素来包装指定的内容。

jQuery wrapInner()示例2

<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").wrapInner("<em><b><marquee></marquee></b></em>");
    });
});
</script>
</head>
<body>
<p>Hello Guys!</p>
<p>This is lidihuo.com</p>
<button>Wrap a emphasized element around the content of each p element</button>
</body>
</html>
输出:

Hello Guys!

This is lidihuo.com

在上面的示例中,我们总共使用了三个标签:
<em> ... </em>标签 <b> ... </b>标签 <marquee> ... </marquee>标记

jQuery wrapInner()示例3

<!DOCTYPE html>
<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

      <script type="text/javascript" language="javascript">
         $(document).ready(function() {
            $("div").click(function () {
               var content = "<marquee><b></b></marquee>";
               $(this).wrapInner( content );
            });
         });
      </script>

      <style>
         .div{ margin:12px;padding:12px; border:2px solid #666; width:100px;}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="destination">lidihuo.com</div>
<div class="div" style="background-color:lightpink;">JAVA</div>
<div class="div" style="background-color:green;">SQL</div>
<div class="div" style="background-color:lightyellow;">HTML</div>
</body>
</html>
输出:

Click on any square below to see the result:

lidihuo.com
JAVA
SQL
HTML
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4