본문 바로가기

개발하자/자바스크립트

select 한 option 값을 이용해서 배경색바꾸기 onchange사용의 예

<html>
 <head>
 <script>
 window.onload=function(){
 b.onclick=function(){
document.bgColor=sel.value;
 }
 }
 </script>
 </head>
 <body>
 <select id="sel">
 <option>red</option>
 <option>orange</option>
 <option>gray</option>
 </select>
 <input type="button" id="b" value="배경색상변경하기">
 </body>
</html>

 

 

 

<html>
 <head>
 <script>
 window.onload=function(){
 sel.onchange=function(){
document.bgColor=sel.value;
 }
 }
 </script>
 </head>
 <body>
 <select id="sel">
 <option>red</option>
 <option>orange</option>
 <option>gray</option>
 </select>
 </body>
</html>