2006년 12월 14일 목요일

DOM으로 sylte속성을 setAttribute로 설정할 때 문제점

역시 IE의 문제점입니다. IE에서 element에 속성을 줄 때 (setAttribute로) style속성이 먹히지 않습니다.
검색을하다보니 quirksmode의 Bug Report에 올라와 있더군요.

Problem
다음과 같은 html코드가 있다고 가정을 하구요.
User inserted image
JavaScript 코드로 id가 "test"인 div에 "color:red;"라는 style을 줍니다.
User inserted image
Mozilla 계열의 브라우저에서는 해당 속성이 적용되나 IE에서는 적용이 되지 않습니다.

Solution
IE인 경우는 분기하여 element.style.setAttribute를 사용합니다. 이 때 주의할 점은 속성으로 'cssText'가 name으로 들어가야 한다는 점입니다.
User inserted image

출처 : setAttribute does not work when used with the style attribute


댓글 없음:

댓글 쓰기