<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
div{
border : 1px dotted blue;
margin : 10px;
padding : 10px;
height : 100px;
}
</style>
<script>
window.onload = function(){
var vd2 = document.getElementById("d2")
var vd2 = document.querySelector('#d2')
setInterval(function(){
vr = parseInt(Math.random()*256);
vg = parseInt(Math.random()*256);
vb = parseInt(Math.random()*256);
vd2.style.background =
'rgb(' + vr +',' + vg +',' + vb +')';
}, 500)
}
function randomColor(){
vr = parseInt(Math.random()*256);
vg = parseInt(Math.random()*256);
vb = parseInt(Math.random()*256);
vr16 = vr.toString(16);
vg16 = vr.toString(16);
vb16 = vr.toString(16);
var vdiv = document.querySelector('div');
vdiv.style.background='#' + vr16 + vg16 + vb16;
}
</script>
</head>
<body>
<pre>
버튼을 클릭 할때마다 div의 색상을 변경
script에서 html 요소 검색
document.getElementById('id이름')
document.getElementsByName('div')
document.getElementsByClassName('class이름')
document.querySelector('#id이름')
document.querySelector('.class이름')
document.querySelector('태그이름')
document.querySelectorAll('.class이름')
document.querySelectorAll('태그이름')
</pre>
<div></div>
<button type = "button" onclick="randomColor()">확인</button>
<br></br>
<div id="d2">일정시간이 지날때마다 배경색 변경<br>
setInterval(function(){ }, 500)
</div>
</body>
</html>
