본문 바로가기

카테고리 없음

Shorthand of getElementById()

getElementById()의 다른 표현들




<!DOCTYPE html>
<html>
<body>

<p id="demo">Hello World!</p> 

<script>
//요소의 id는 window객체에 저장된다. 
//그러므로 아래 3 라인의 내용은 동일하다
demo.style.backgroundColor = 'red';
window.demo.style.backgroundColor = 'red';
document.getElementById('demo').style.backgroundColor = 'red';
</script>

</body>
</html>