jQuery text() 메서드

❮ jQuery HTML/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

직접 해보십시오 - 예


반환 선택한 요소의 텍스트 내용을 반환하는 방법입니다.


을 사용하여 텍스트 내용 설정 기능을 사용하여 선택한 요소의 텍스트 내용을 설정합니다.


❮ jQuery HTML/CSS 메소드