<html>
<head>
<title></title>
<script language="javascript">
function pageInit(){//定位到第二个选项
var optionitems = document.getElementById("book");
for(var i=0;i<optionitems.length;i++){
if(optionitems.options[i].value =="2"){
optionitems.options[i].selected = true;
}
}
}
//页面加载时候执行pageInit()函数,等同与body元素中的onload属性
window.onload =pageInit;
//选项变更时,输出option的文本
function selectBook(){
var optionitems = document.getElementById("book");
for(var i=0;i<optionitems.length;i++){
if(optionitems.options[i].selected){
alert(optionitems.options[i].text);
}
}
}
function addOption(){
var optionitems = document.getElementById("book");
//new Option(text,value);
optionitems.options[optionitems.length] = new Option("php","4");
}
function delOption(){
var optionitems = document.getElementById("book");
//删除的时候,只需要option对象赋值为空即可
optionitems.options[0] = null;
}
function clearOption(){
var optionitems = document.getElementById("book");
optionitems.length = 0;
}
</script>
</head>
<body>
<select name="book" οnchange="javascript:selectBook();">
<option value="1">java</option>
<option value="2">c++</option>
<option value="3">.net</option>
</select>
<input type="button" value="增加option" οnclick="javascript:addOption();">
<input type="button" value="删除option" οnclick="javascript:delOption();">
<input type="button" value="清空option" οnclick="javascript:clearOption();">
</body>
</html>