javascript style

이승훈·2024년 9월 25일

참조

element.style.zIndex 

같은 스타일 속성을 자바스크립트에서 참조할 때 빈 문자열 ""이 나오는 이유는 인라인 스타일로 설정되지 않은 CSS 속성값은 style 객체에서 직접적으로 접근할 수 없기 때문입니다.
하지만, CSS 파일이나 style 태그로 정의된 스타일 시트에서 설정된 속성 값을 자바스크립트로 가져오고 싶다면, getComputedStyle() 을 사용해야 합니다.

const element = document.getElementById('myElement');
const computedStyle = window.getComputedStyle(element);
const zIndex = computedStyle.zIndex;
console.log('Computed z-index:', zIndex);

설정

element.style.zIndex = 3;
profile
안녕하세요!

0개의 댓글