jQuery API 返回首页目录 | jQuery API 中英文对照版
wrap(elem)
wrap(elem)

把所有匹配的元素用其他元素的结构化标记包装起来。

这种包装对于在文档中插入额外的结构化标记最有用,而且它不会破坏原始文档的语义品质。

这个函数的原理是检查提供的第一个元素并在它的代码结构中找到最上层的祖先元素--这个祖先元素就是包装元素。 当HTML标记代码中的元素包含文本时无法使用这个函数。

因此,如果要添加文本应该在包装完成之后再行添加。

返回值:jQuery

参数:

  • elem (Element): 用于包装目标元素的DOM元素
 

示例:

$("p").wrap( document.getElementById('content') );

HTML 代码:

<p>Test Paragraph.</p><div id="content"></div>

结果:

<div id="content"><p>Test Paragraph.</p></div>

 
wrap( elem )

Wrap all matched elements with a structure of other elements. This wrapping process is most useful for injecting additional stucture into a document, without ruining the original semantic qualities of a document.

This works by going through the first element provided and finding the deepest ancestor element within its structure - it is that element that will en-wrap everything else.

This does not work with elements that contain text. Any necessary text must be added after the wrapping is done.

Return value: jQuery
Parameters:

  • elem (Element): A DOM element that will be wrapped around the target.
 

Example:

 $("p").wrap( document.getElementById('content') );  
Before:
 <p>Test Paragraph.</p><div id="content"></div>  
Result:
 <div id="content"><p>Test Paragraph.</p></div>  
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog