English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
prepend()メソッドを使用して指定された内容を各選択された要素の先頭に挿入します(最初の子要素として)。
選択された要素の末尾に内容を挿入するには、append()メソッド。
前置内容を挿入する:
$.selector.prepend(content)
関数を使用して内容を追加する:
$.selector.prepend(function(index, html))
すべての段落の前にテキスト内容を追加します:
$("button").click(function(){ $("p").prepend("Hello world"); });テストして‹/›
すべての段落の前にHTMLを追加します:
$("button").click(function(){ $("p").prepend("<b>Hello world</b>); });テストして‹/›
この例では、document.createTextNode()を使用してテキストノードを作成し、それをすべての<p>要素の前に追加します:
$("button").click(function(){ $("p").prepend(document.createTextNode("Hello world")); });テストして‹/›
関数を使用して内容を追加する:
$("button").click(function(){ $("p").prepend(function(i){ return "<b>This p element has index " + i; + i + "</b>"; }); });テストして‹/›
引数 | 説明 |
---|---|
content | 指定する各選択された要素の先頭に挿入する内容(HTMLタグを含むことができます) 可能な値:
|
function(index, html) | 指定一个函数,该函数返回要插入的内容
|