Home > Article > Web Front-end > Usage examples of wrapInner() method in jQuery
This article mainly introduces the usage of wrapInner() method in jQuery. It analyzes the function and definition of wrapInner() method and the usage skills of wrapping matching elements with examples. Friends who need it can refer to it
The example in this article describes the usage of wrapInner() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:
This method uses the specified HTML content or element to wrap the matching element content.
Syntax 1:
Use the specified DOM element to wrap the content of the matching element.
The code is as follows:
$(selector).wrapInner(elem)
Parameter list:
Example:
The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.jb51.net/" /> <head> <title>脚本之家</title> <style type="text/css"> p { width:200px; height:200px; border:1px solid blue; } p { width:150px; height:150px; border:1px solid red; } </style> <script type="text/ javascript " src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $( document ).ready(function(){ $("button").click(function(){ $("p").wrapInner(document.createElement("p")); }); }); </script> </head> <body> <p>这是p的内容,将会被包裹</p> <p>这是p的内容,将会被包裹</p> <button>用p包裹每个段落</button> </body> </html>
Syntax 2:
Use function Returns the element used to wrap the content of the matching element.
The code is as follows:
$(selector).wrapInner(function)
Parameter list:
实例:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.jb51.net/" /> <head> <title>脚本之家</title> <style type="text/css"> p { width:200px; height:200px; border:1px solid blue; } p { width:150px; height:150px; border:1px solid red; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").wrapInner(function(){ return "<p></p>" }); }); }); </script> </head> <body> <p>这是p的内容,将会被包裹</p> <p>这是p的内容,将会被包裹</p> <button>用p包裹每个段落</button> </body> </html>
语法三:
用html标记代码包裹匹配元素的内容。
代码如下:
$(selector).wrapInner(html)
参数列表:
实例:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.jb51.net/" /> <head> <title>脚本之家</title> <style type="text/css"> p { width:200px; height:200px; border:1px solid blue; } p { width:150px; height:150px; border:1px solid red; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").wrapInner("<p></p>"); }); }); </script> </head> <body> <p>这是p的内容,将会被包裹</p> <p>这是p的内容,将会被包裹</p> <button>用p包裹每个段落</button> </body> </html>
The above is the detailed content of Usage examples of wrapInner() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!