首頁  >  文件處理  > before(content|fn)

返回值:jQuery before(content|fn)

概述

在每個匹配的元素之前插入內容。

參數

content String, Element, jQuery V1.0

插入到每個目標后的內容

function Function V1.4

函式必須返回一個html字串。

示例

描述:

在所有段落之前插入一些HTML標記程式碼。

HTML 程式碼:

<p>I would like to say: </p>
jQuery 程式碼:

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

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

描述:

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

HTML 程式碼:

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

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

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

描述:

在所有段落中前插入一個jQuery對像(類似於一個DOM元素陣列)。

HTML 程式碼:

<p>I would like to say: </p><b>Hello</b>
jQuery 程式碼:

$("p").before( $("b") );
結果:

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