getElementByID、createElement、appendChild几个DHTML元素
更新时间:2008年06月25日 21:17:24 作者:
WEB标准下可以通过getElementById(), getElementsByName(), and getElementsByTagName()访问
appendChild方法的使用
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script language="javascript">
//生成与输入内容匹配行
function setNames() {
completeBody = document.getElementById("complete_body");
var row, cell, txtNode;
//var nextNode = names[i].firstChild.data;
row = document.createElement("tr");
cell = document.createElement("td");
cell.setAttribute("bgcolor", "#FFFAFA");
cell.setAttribute("border", "0");
//txtNode = document.createTextNode(nextNode);
alert("sdf");
var newText = document.createTextNode("This is the second paragraph.");
//txtNode=document.createElement("div");
alert("sdf1");
cell.appendChild(newText);
alert("sdf2");
row.appendChild(cell);
completeBody.appendChild(row);
}
</script>
<body>
<input type="submit" name="sdf" onclick="setNames()">
<table id="complete_table" bgcolor="#FFFAFA" border="0"
cellspacing="0" cellpadding="0" />
<tbody id="complete_body"></tbody>
</table>
</body>
</html>
=================================
createElement
<html>
<head>
<title>createElement</title>
<script language="javascript">
<!--
var i=0 ;
function addInput() {
var o = document.createElement("input");
o.type = "button" ;
o.value = "按钮" + i++ ;
o.attachEvent("onclick",addInput);
document.body.appendChild(o);
o = null;
}
//-->
</script>
</head>
<body onload="addInput();">
</body>
</html>
最新评论