jQuery html()メソッド
例
すべての<p>要素の内容を変更します。
$("button").click(function(){
$("p").html("Hello <b>world</b>!");
});
定義と使用法
html()メソッドは、選択した要素のコンテンツ(innerHTML)を設定または返します。
このメソッドを使用してコンテンツを返す場合、最初に一致した要素のコンテンツを返します。
このメソッドを使用してコンテンツを設定すると、一致したすべての要素のコンテンツが上書きされます。
ヒント:選択した要素のテキストコンテンツのみを設定または返すには、text()メソッドを使用します。
構文
返品内容:
$(selector).html()
コンテンツを設定します。
$(selector).html(content)
関数を使用してコンテンツを設定します。
$(selector).html(function(index,currentcontent))
Parameter | Description |
---|---|
content | Required. Specifies the new content for the selected elements (can contain HTML tags) |
function(index,currentcontent) | Optional. Specifies a function that returns the new content for the selected elements
|
自分で試してみてください-例
の内容を返す方法。
を使用して、選択したすべての要素の内容を設定します。