jQuery before()メソッド

❮jQueryHTML/ CSSメソッド

各<p>要素の前にコンテンツを挿入します。

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

定義と使用法

before()メソッドは、選択された要素の前(前)に指定されたコンテンツを挿入します。

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


構文

$(selector).before(content,function(index))

Parameter Description
content Specifies the content to insert (can contain HTML tags)

Possible values:

  • HTML elements
  • jQuery objects
  • DOM elements
function(index) Optional. Specifies a function that returns the content to insert
  • index - Returns the index position of the element in the set

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


()メソッドを使用してコンテンツを挿入します。


挿入する関数を使用して、選択した要素の前にコンテンツを挿入する方法。


❮jQueryHTML/ CSSメソッド