2016년 10월 17일 월요일

예제3(Element 생성하는 방법)

<html>
<head>
<title>||Working with elements||</title>
</head>
<script type="text/javascript">
var my_div = null;
var newDiv = null;
function addElement() {
// create a new div element
// and give it some content
newDiv = document.createElement("div");
<!-- 텍스트 생성 -->
newContent = document.createTextNode("Hi there and greetings!");
newDiv.appendChild(newContent);
//add the text node to the newly created div.
// add the newly created element and it's content into the DOM
my_div = document.getElementById("org_div1");
document.body.insertBefore(newDiv, my_div); <!--my_div 앞에 newDiv 넣기 -->
}
</script>
<body onload="addElement()"> <!-- 페이지 로드될 때 함수 실행 -->
<div id='org_div1'>
The text above has been created dynamically.
</div>
</body>
</html>

댓글 없음:

댓글 쓰기