jQuery attr() 메서드

❮ jQuery HTML/CSS 메소드

예시

이미지의 너비 속성 설정:

$("button").click(function(){
  $("img").attr("width","500");
});

정의 및 사용

attr() 메서드는 선택한 요소의 속성과 값을 설정하거나 반환합니다.

이 메서드를 사용 하여 속성 값을 반환하면 FIRST 일치 요소의 값을 반환합니다 .

이 방법을 사용하여 속성 값을 설정 하면 일치하는 요소 집합에 대해 하나 이상의 속성/값 쌍을 설정합니다.


통사론

속성 값을 반환합니다.

$(selector).attr(attribute)

속성 및 값 설정:

$(selector).attr(attribute,value)

함수를 사용하여 속성 및 값 설정:

$(selector).attr(attribute,function(index,currentvalue))

여러 속성 및 값 설정:

$(selector).attr({attribute:value, attribute:value,...})

Parameter Description
attribute Specifies the name of the attribute
value Specifies the value of the attribute
function(index,currentvalue) Specifies a function that returns the attribute value to set
  • index - Receives the index position of the element in the set
  • currentvalue - Receives the current attribute value of selected elements

직접 해보십시오 - 예


반환 요소에 대한 속성 값을 반환하는 방법입니다.


함수를 사용하여 요소의 속성 값을 설정하는 방법입니다.


설정 요소에 대해 여러 속성/값을 설정하는 방법입니다.


❮ jQuery HTML/CSS 메소드