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

向每个匹配的元素内部前置内容。 这是向所有匹配元素内部的开始处插入内容的最佳方式。

返回值:jQuery

参数:

  • content (<Content>): 要插入到目标元素内部前端的内容
 
示例:

向所有段落中前置一些HTML标记代码。

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

HTML 代码:

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

结果:

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

示例:

向所有段落中前置一个元素。

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

HTML 代码:

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

结果:

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

示例:

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

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

HTML 代码:

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

结果:

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

 

prepend( content )

Prepend content to the inside of every matched element.

This operation is the best way to insert elements inside, at the beginning, of all matched elements.

Return value: jQuery
Parameters:

  • content (<Content>): Content to prepend to the target.
 

Example:

Prepends some HTML to all paragraphs.

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

Before:

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

Result:

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


Example:

Prepends an Element to all paragraphs.

 $("p").prepend( $(".foo")[0] );  
Before:
 <p>I would like to say: </p>
   <p>I would like to say: </p> 
  <b class="foo">Hello</b>
   <b class="foo">Good By</b>  
Result:
 <p><b class="foo">Hello</b>I would like to say: </p>
   <p><b class="foo">Hello</b>I would like to say: </p>

(note the [0], that indicates that we only prepend the first element of $(".foo"))

Example:

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

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