jQuery wrap() 方法
实例
在每个 <p> 元素外包裹一个 <div> 元素:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p").wrap("<div></div>");});});</script><style>div{background-color: yellow;}</style></head><body><p>这是一个段落</p><p>这是另外一个段落</p><button>在每个 p 元素外包裹一个 div 元素</button></body></html>
定义与用法
wrap() 方法在每个选定元素外包裹指定的 HTML 元素。
语法
$(selector).wrap(wrappingElement,function(index))
| 参数 | 描述 |
|---|---|
| wrappingElement | 必填。指定要包裹每个选定元素的 HTML 元素 可能的值为:
|
| function(index) | 可选。 指定返回包裹元素的函数
|