<!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>