<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
        <script type="text/javascript">
               function Init() {

                       for (var i = 0; i< 10; i++) {
                              //div 객체 생성
                              var div = document.createElement("div");
                              div.innerText= i;
                              //css설정
                              div.style.border= "1px solid black";
                              div.style.margin= "5px";
                              div.style.width= "100px";
                              div.style.height= "100px";
                              div.style.backgroundColor= "lightgreen";
                              div.style.float= "left";
                              div.onclick= function () { //익명 메서드
                                      //event.srcElement => 이벤트 발생 객체
                                      event.srcElement.style.backgroundColor= "yellow";
                                      event.srcElement.style.border= "1px dotted red";
                                      //event.srcElement.style.float = "left";
                              };
                              //** 바디의 마지막 자식으로 div 객체 추가
                              document.body.appendChild(div);
                       }
               }
        </script>
</head>
<body onload="Init();">
</body>
</html>

 

 

출처 ㅣ http://cafe.naver.com/jobstartgogo/20696

+ Recent posts