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搞代码网其它相关文章!