jQuery prependTo()メソッド
例
各<p>要素の先頭に<span>要素を挿入します。
$("button").click(function(){
$("<span>Hello World!</span>").prependTo("p");
});
定義と使用法
prependTo()メソッドは、選択した要素の先頭にHTML要素を挿入します。
ヒント:選択した要素の最後にHTML要素を挿入するには、 appendTo()メソッドを使用します。
構文
$(content).prependTo(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 at the beginning of the selected elements. |
selector | Required. Specifies on which elements to prepend the content to |
自分で試してみてください-例
prependTo()メソッドを使用して、選択した各要素の先頭に既存の要素を挿入する方法。