包装(Html)方法用指定的HTML内容包装每个匹配的元素。
这种包装过程对于向文档中注入附加结构最有用,而不会破坏文档的原始语义质量。
selector.wrap( html )
下面是此方法使用的所有参数的说明
elem - 动态创建并环绕每个目标的HTML字符串。
链接:https://www.learnfk.comhttps://www.learnfk.com/jquery/dom-wrap-html.html
来源:LearnFk无涯教程网
下面是一个简单的示例,一个简单的示例说明了该方法的用法。当单击任何正方形时,这会将目标正方形与正方形进行包装(-
<html> <head> <title>The jQuery Example</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("div").click(function () { var content='<div class="div"></div>'; $("#destination").wrap( content ); }); }); </script> <style> .div{ margin:10px;padding:12px; border:2px solid #666; width:60px;} </style> </head> <body> <p>Click on any square below to see the result:</p> <div class="div" id="destination">THIS IS TEST</div> <div class="div" style="background-color:blue;">ONE</div> <div class="div" style="background-color:green;">TWO</div> <div class="div" style="background-color:red;">THREE</div> </body> </html>
这将产生以下输出-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)