【javascript】select操作实例

window.onload=function(){
     //创建select
     var select1= document.createElement("select");
     select1.id="select1";
    
     for(var i=0;i<5;i++){
     //创建option
     var option=document.createElement("option");
     //var option=new option();
     option.value=i;
     option.text="option"+i;
    
     //添加option 三选一
     select1.add(option);
     //select1.options.add(option);
     //select1.appendChild(option);

     }

     
        document.body.appendChild(select1);

        var index=select1.selectedIndex;//获得选择的option索引
        var option_selected=select1.options[select1.selectedIndex];//获得选定的option

        //插入option
        select1.options.add(new Option('add1','add1'));

        //移除option 方法二选一
        //select1.options.remove(1);
        select1.remove(1);

        //清除option 方法二选一
        //select1.options.length=0;
        //select1.length=0;
}
原文地址:https://www.cnblogs.com/zspbolg/p/3737341.html