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

在每个匹配的元素之前插入内容。

返回值:jQuery

参数:

  • content (<Content>): 插入到每个目标前的内容
 
示例:

在所有段落之前插入一些HTML标记代码。

$("p").before("<b>Hello</b>"); 

HTML 代码:

<p>I would like to say: </p>

结果:

<b>Hello</b><p>I would like to say:   </p>
示例:

在所有段落之前插入一个元素。

$("p").before( $("#foo")[0] ); 

HTML 代码:

<p>I would like to say: </p><b   id="foo">Hello</b>

结果:

<b id="foo">Hello</b><p>I would like to say:   </p>
示例:

在所有段落中前插入一个jQuery对象(类似于一个DOM元素数组)。

$("p").before( $("b") ); 

HTML 代码:

<p>I would like to say:   </p><b>Hello</b>

结果:

<b>Hello</b><p>I would like to say:   </p>
 
before( content )

Insert content before each of the matched elements.

Return value: jQuery
Parameters:

  • content (<Content>): Content to insert before each target.
 

Example:

Inserts some HTML before all paragraphs.

 $("p").before("<b>Hello</b>");  
Before:
 <p>I would like to say: </p>  
Result:
 <b>Hello</b><p>I would like to say: </p>

Example:

Inserts an Element before all paragraphs.

 $("p").before( $("#foo")[0] );  

Before:

 <p>I would like to say: </p><b id="foo">Hello</b>  

Result:

 <b id="foo">Hello</b><p>I would

Example:

Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs.

 $("p").before( $("b") );  
Before:
 <p>I would like to say: </p><b>Hello</b>  
Result:
 <b>Hello</b><p>I would like to say
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog