jQuery text()メソッド

❮jQueryHTML/ CSSメソッド

すべての<p>要素のテキストコンテンツを設定します。

$("button").click(function(){
  $("p").text("Hello world!");
});

定義と使用法

text()メソッドは、選択した要素のテキストコンテンツを設定または返します。

このメソッドを使用してコンテンツを返す場合、一致したすべての要素のテキストコンテンツを返します(HTMLマークアップは削除されます)。

このメソッドを使用してコンテンツを設定すると、一致したすべての要素のコンテンツが上書きされます。

ヒント:選択した要素のinnerHTML(テキスト+ HTMLマークアップ)を設定または返すには、html()メソッドを使用します。


構文

テキストコンテンツを返す:

$(selector).text()

テキストコンテンツを設定します。

$(selector).text(content)

関数を使用してテキストコンテンツを設定します。

$(selector).text(function(index,currentcontent))

Parameter Description
content Required. Specifies the new text content for the selected elements

Note: Special characters will be encoded
function(index,currentcontent) Optional. Specifies a function that returns the new text content for the selected elements
  • index - Returns the index position of the element in the set
  • currentcontent - Returns current content of selected elements

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


を返す選択した要素のテキストコンテンツを返す方法。


設定する関数を使用して、選択した要素のテキストコンテンツを設定します。


❮jQueryHTML/ CSSメソッド