<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="content-Type" content="text/html; charset=utf-8"/>
<head>
<title>添加节点</title>
<script type="text/javascript">
function addOption(){
//找到下拉选项
var selNode = document.getElementsByTagName("select")[0];
//产生一个新的节点
var optNode = document.createElement("option");
//设置value属性
optNode.setAttribute("value","小学");
//设置文本节点
var textNdoe = document.createTextNode("小学")
//追加子节点
optNode.appendChild(textNdoe);
selNode.appendChild(optNode);
}
//----------HTML DOM 实现---------------------------------------
function addOptionByHTMLDOM(){
//找到下拉选项
var selNode = document.getElementsByTagName("select")[0];
//产生option对象
var optNode = new Option("小学","primary school")
//将option加入select
selNode.add(optNode);
}
</script>
</head>
<body>
<input type="button" value="添加选项" onclick="addOptionByHTMLDOM()"/>
<select name="edu" id="edu">
<option value="博士">博士</option>
<option value="硕士">硕士</option>
<option value="本科">本科</option>
<option value="幼儿园">幼儿园</option>
</select>
</body>
</html>