• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

jQuery wrap()方法怎么用?

jquery 搞代码 4年前 (2021-12-27) 25次浏览 已收录 0个评论

jQuery wrap()方法怎么用?下面本篇文章给

来源gao!daima.com搞$代!码网

大家介绍一下jQuery wrap()方法的使用。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

jQuery wrap()方法怎么用?

wrap()方法:是指用某个标签将某个元素包起来,即在每个匹配的元素外层包上一个html元素。

有两种使用方法:

  • wrap(wrappingElement):其中wrappingElement可以是一个HTML片段,选择表达式,jQuery对象,或者DOM元素,用来包在匹配元素的外层。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    </head>

    <body>
        <div class="container">
            <div class="inner">Hello</div>
            <div class="inner">Goodbye</div>
        </div>
    </body>
    
    <script type="text/javascript">
        $(function(){
            $('.inner').wrap('<div class="new"></div>');
        });
    </script>

</html>

浏览器中显示的代码:

  • wrap(function(index)):回调函数,返回用于包裹匹配元素的 HTML 内容或 jQuery 对象。index 参数表示匹配元素在集合中的集合。该函函数内的this指向集合中的当前元素。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    </head>

    <body>
        <div class="container">
            <div class="inner">Hello</div>
            <div class="inner">Goodbye</div>
        </div>
    </body>
    
    <script type="text/javascript">
        $(function(){
            $('.inner').wrap(function(){
                return "<div class='>"+$(this).text()+"<'/div>"
            });
        });
    </script>

</html>

浏览器中显示的代码:

更多web开发知识,请查阅 搞代码网 !!

以上就是jQuery wrap()方法怎么用?的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:jQuery wrap()方法怎么用?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址