JavaScript style 속성의 단위
style 속성을 통해 CSS 속성 값을
지정할 때는 단위를 명시해야 합니다:
elem.style.width = '100px';
값을 읽어올 때도 단위가 포함된 상태로 가져옵니다:
let width = elem.style.width;
console.log(width); // '100px'를 출력합니다;
필요한 경우 parseInt를 사용하여
단위를 제거할 수 있습니다:
let width = elem.style.width;
console.log(parseInt(width)); // 100을 출력합니다
소수값이 예상되는 경우,
parseFloat 함수를 사용할 수 있습니다:
let width = elem.style.width;
console.log(parseFloat(width));
div 요소와 button이 주어져 있습니다. 버튼을 클릭하면
div의 너비를 400px로,
높이를 300px로 설정하세요.
div 요소가 주어져 있습니다:
<div id="elem" style="width: 300px; height: 200px;">
text
</div>
button도 주어져 있습니다. 버튼을 클릭하면 단위 없이 div의 너비와 높이를 출력하세요.
div 요소가 주어져 있습니다:
<div id="elem" style="font-size: 1.5em;">
text
</div>
button도 주어져 있습니다. 버튼을 클릭하면 단위 없이 div의 글꼴 크기를 출력하세요.