jQuery 기타 데이터() 메서드

❮ jQuery 기타 메소드

예시

데이터를 <div> 요소에 첨부한 다음 데이터를 검색합니다.

$("#btn1").click(function(){
  $("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
  alert($("div").data("greeting"));
});

정의 및 사용

data() 메서드는 선택한 요소에 데이터를 첨부하거나 데이터를 가져옵니다.

팁: 데이터를 제거하려면 removeData() 메서드를 사용하십시오.


요소에서 데이터 반환

선택한 요소에서 첨부된 데이터를 반환합니다.

통사론

$(selector).data(name)

Parameter Description
name Optional. Specifies the name of data to retrieve.
If no name is specified, this method will return all stored data for the element as an object


요소에 데이터 첨부

선택한 요소에 데이터를 첨부합니다.

통사론

$(selector).data(name,value)

Parameter Description
name Required. Specifies the name of data to set
value Required. Specifies the value of data to set

개체를 사용하여 요소에 데이터 첨부

이름/값 쌍이 있는 개체를 사용하여 선택한 요소에 데이터를 첨부합니다.

통사론

$(selector).data(object)

Parameter Description
object Required. Specifies an object containing name/value pairs

❮ jQuery 기타 메소드