資源描述:
《js對(duì)select動(dòng)態(tài)添加和刪除OPTION.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、js對(duì)select動(dòng)態(tài)添加和刪除OPTIONcreateSelect();addOption();//removeAll();//removeOne();//動(dòng)態(tài)創(chuàng)建selectfunctioncreateSelect(){varmySelect=document.createElement("select");mySelect.id="mySelect";document.body.appendChild(mySelect);}//添加選項(xiàng)optionfuncti
2、onaddOption(){//根據(jù)id查找對(duì)象,varobj=document.getElementById('mySelect');//添加一個(gè)選項(xiàng)//obj.add(newOption("文本","值"));//這個(gè)只能在IE中有效obj.options.add(newOption("text","0"));//這個(gè)兼容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'));}//刪除所有選項(xiàng)optionfunctionremoveAll(){varobj=document.getElementById('mySelect');obj.options.length=0;}//刪除一個(gè)選項(xiàng)optionfunctionremoveOne(){varobj=document.getElementById('mySelect');//index,要?jiǎng)h除選項(xiàng)的序號(hào)
4、,這里取當(dāng)前選中選項(xiàng)的序號(hào)varindex=obj.selectedIndex;obj.options.remove(index);}//獲得選項(xiàng)option的值functiongetOptionVal(){varobj=document.getElementById('mySelect');varindex=obj.selectedIndex;//序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)varval=obj.options[index].value;}//獲得選項(xiàng)option的文本functiongetOptionText(){varo
5、bj=document.getElementById('mySelect');varindex=obj.selectedIndex;//序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)varval=obj.options[index].text;}//修改選項(xiàng)optionfunctionupdateOption(){varobj=document.getElementById('mySelect');varindex=obj.selectedIndex;//序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)varval=obj.options[index]=newOpt
6、ion("新文本","1");}//刪除selectfunctionremoveSelect(){varmySelect=document.getElementById("mySelect");mySelect.parentNode.removeChild(mySelect);}