jQuery insertBefore()メソッド

❮jQueryHTML/ CSSメソッド

各<p>要素の前に<span>要素を挿入します。

$("button").click(function(){
  $("<span>Hello world!</span>").insertBefore("p");
});

定義と使用法

insertBefore()メソッドは、選択した要素の前にHTML要素を挿入します。

ヒント:選択した要素の後にHTML要素を挿入するには、 insertAfter()メソッドを使用します。


構文

$(content).insertBefore(selector)

Parameter Description
content Required. Specifies the content to insert (must contain HTML tags).

Note: If content is an existing element, it will be moved from its current position, and inserted before the selected elements.
selector Required. Specifies where to insert the content

自分で試してみてください-例


insertBefore()メソッドを使用して、選択した各要素の前に既存の要素を挿入する方法。


❮jQueryHTML/ CSSメソッド