首頁  >  文件處理  > insertBefore(content)

返回值:jQuery insertBefore(content)

概述

把所有匹配的元素插入到另一個、指定的元素元素集合的前面。

實際上,使用這個方法是顛倒了常規的$(A).before(B)的操作,即不是把B插入到A前面,而是把A插入到B前面。

在jQuery 1.3.2中,appendTo, prependTo, insertBefore, insertAfter, 和 replaceAll這個幾個方法成為一個破壞性操作,要選擇先前選中的元素,需要使用end()方法,參見 appendTo 方法的例二。

參數

content String V1.0

用於匹配元素的jQuery表達式

示例

描述:

把所有段落插入到一個元素之前。與 $("#foo").before("p")相同。

HTML 程式碼:

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

$("p").insertBefore("#foo");
結果:

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