요소의 속성 값을 가져오는 방법 - .getAttribute()

getAttribute는 선택한 요소(element)의 특정 속성(attribute)의 값을 가져옵니다.

.getAttribute()

.getAttribute()는 선택한 요소(element)의 특정 속성(attribute)의 값을 가져옵니다.

 

기본형

element.getAttribute( 'attributename' );

 

예를 들어

var abc = document.getElementById( 'xyz' ).getAttribute( 'title' );

는 id의 값이 xyz인 요소의 title 속성 값을 변수 abc에 저장합니다.

 

예제

id의 값이 abc인 a 요소의 href 속성의 값을 가져와서 출력합니다.

<p><a id="abc" href="https://www.google.com">Google</a></p>
var Href = document.getElementById( 'abc' ).getAttribute( 'href' );
document.write( '<p>href : ' + Href + '</p>' );

로딩 중... 잠시만 기다려주세요.
자바스크립트를 허용해주세요.

Post a Comment