이벤트(Event) - DHTML(Dynamic HTML
DHTML : 태그에서 이벤트가 발생될 경우 태그에 대한 Element 객체를 조작하여 동적인 페이지 제공하는 기능
<style type="text/css">
#itemListDiv div {
margin: 5px;
}
</style>
</head>
<body>
<h1>이벤트(Event) - DHTML(Dynamic HTML)</h1>
<hr>
<p>DHTML : 태그에서 이벤트가 발생될 경우 태그에 대한 Element 객체를 조작하여
동적인 페이지 제공하는 기능</p>
<hr>
<button type="button" id="addBtn">아이템 추가</button>
<div id="itemListDiv"></div>
<script type="text/javascript">
var count=0;
document.getElementById("addBtn").onclick=function() {
count++;
var newItem=document.createElement("div");
newItem.setAttribute("id", "item_"+count);
var html="아이템["+count+"] ";
html+="<button type='button' onclick='removeItem("+count+");'>삭제</button>";
newItem.innerHTML=html;
document.getElementById("itemListDiv").appendChild(newItem);
}
function removeItem(cnt) {
var removeElement=document.getElementById("item_"+cnt);
document.getElementById("itemListDiv").removeChild(removeElement);
}
</script>
</body>