createSelect();addOption();//removeAll();//removeOne();//动态创建selectf"> createSelect();addOption();//removeAll();//removeOne();//动态创建selectf" />
欢迎来到天天文库
浏览记录
ID:53879017
大小:14.50 KB
页数:2页
时间:2020-04-10
《js对select动态添加和删除OPTION.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、js对select动态添加和删除OPTIONcreateSelect();addOption();//removeAll();//removeOne();//动态创建selectfunctioncreateSelect(){varmySelect=document.createElement("select");mySelect.id="mySelect";document.body.appendChild(mySelect);}//添加选项optionfuncti
2、onaddOption(){//根据id查找对象,varobj=document.getElementById('mySelect');//添加一个选项//obj.add(newOption("文本","值"));//这个只能在IE中有效obj.options.add(newOption("text","0"));//这个兼容IE与firefoxobj.options.add(newOption('javascript','1'));obj.options.add(newOption('css','2'));obj.opti
3、ons.add(newOption('jquery','3'));obj.options.add(newOption('html','4'));}//删除所有选项optionfunctionremoveAll(){varobj=document.getElementById('mySelect');obj.options.length=0;}//删除一个选项optionfunctionremoveOne(){varobj=document.getElementById('mySelect');//index,要删除选项的序号
4、,这里取当前选中选项的序号varindex=obj.selectedIndex;obj.options.remove(index);}//获得选项option的值functiongetOptionVal(){varobj=document.getElementById('mySelect');varindex=obj.selectedIndex;//序号,取当前选中选项的序号varval=obj.options[index].value;}//获得选项option的文本functiongetOptionText(){varo
5、bj=document.getElementById('mySelect');varindex=obj.selectedIndex;//序号,取当前选中选项的序号varval=obj.options[index].text;}//修改选项optionfunctionupdateOption(){varobj=document.getElementById('mySelect');varindex=obj.selectedIndex;//序号,取当前选中选项的序号varval=obj.options[index]=newOpt
6、ion("新文本","1");}//删除selectfunctionremoveSelect(){varmySelect=document.getElementById("mySelect");mySelect.parentNode.removeChild(mySelect);}
此文档下载收益归作者所有