close

[JavaScript HTML DOM]使用document物件,使用方法document.createElement()

說明:
document.createElement()
新增一個html 元件

用法:
document.createElement(html 元件名稱)

此範例設計說明:
第一步、使用新增一個html元件button createElement("button")
第二步、將此元件,加入到body裡,使用document.body.appendChild(元件)
程式:
<html>
<head>
<title>[JavaScript-HTML_DOM]使用document物件,使用方法document.createElement()</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function myAdd() {
var btn=document.createElement("button");
document.body.appendChild(btn);
}
</script>
</head>
<body>
[JavaScript-HTML_DOM]使用document物件,使用方法document.createElement()
<input type="button" value="新增" onclick="myAdd()"/>
</body>
</html>
圖:

 按下新增後,會多出一個button




相關範例:

參考:
HTML DOM createElement() Method
http://www.w3schools.com/jsref/met_document_createelement.asp

 




 






 















其它文章

arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()