속성 API
속성을 제어하는 API
Element.getAttribute(name);
Element.setAttribute(name, value);
Element.hasAttribute(name);
Element.removeAttribute(name);
속성과 프로퍼티
setAttribute('class', 'important')와 className = 'important'는 같은 결과를 만든다. 하지만 전자는 attribute 방식(속성이라고 부르겠다)이고 후자는 property 방식이다. property 방식은 좀 더 간편하고 속도도 빠르지만 실제 html 속성의 이름과 다른 이름을 갖는 경우가 있다. 그것은 자바스크립트의 이름 규칙 때문이다.
attribute 방식 | property 방식 |
---|---|
class | className |
readonly | readOnly |
rowspan | rowSpan |
colspan | colSpan |
usemap | userMap |
frameborder | frameBorder |
for | htmlFor |
maxlength | maxLength |
심지어 속성과 프로퍼티는 값이 다를수도 있다. 아래 코드를 실행한 결과는 속성과 프로퍼티의 값이 꼭 같은 것은 아니라는 것을 보여준다.
Last updated