Chinaunix首页 | 论坛 | 博客
  • 博客访问: 242713
  • 博文数量: 76
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 745
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-28 16:04
文章分类

全部博文(76)

文章存档

2013年(2)

2010年(21)

2009年(53)

我的朋友

分类: 系统运维

2010-03-23 09:34:55

  1. 1.动态创建select  
  2.   
  3.       function createSelect(){  
  4.           var mySelect = document.createElement("select");  
  5.           mySelect.id = "mySelect";   
  6.           document.body.appendChild(mySelect);  
  7.       }  
  8.   
  9. 2. 添加选项option  
  10.   
  11.      function addOption(){  
  12.          //根据id查找对象,  
  13.           var obj=document.getElementById('mySelect');  
  14.          //添加一个选项  
  15.           obj.add(new Option("文 本","值"));    //这个只能在IE中有效  
  16.           obj.options.add(new Option("text","value")); //这 个兼容IE与firefox  
  17.      }  
  18.   
  19. 3. 删除所有选项option  
  20.   
  21.      function removeAll(){  
  22.            var obj=document.getElementById('mySelect');  
  23.            obj.options.length=null;  
  24.      }  
  25.   
  26. 4. 删除一个选项option  
  27.   
  28. function removeOne(){  
  29.      var obj=document.getElementById('mySelect');  
  30.      //index,要删除选项的序号,这里取当前选中选项的序号  
  31.       var index=obj.selectedIndex;  
  32.      obj.options.remove(index);  
  33. }  
  34.   
  35. 5. 获得选项option的值  
  36.   
  37.      var obj=document.getElementById('mySelect');  
  38.   
  39.      var index=obj.selectedIndex; //序号,取当前选中选项的序号  
  40.   
  41.      var val = obj.options[index].value;  
  42.   
  43. 6. 获得选项option的文本  
  44.   
  45.      var obj=document.getElementById('mySelect');  
  46.   
  47.      var index=obj.selectedIndex; //序号,取当前选中选项的序号  
  48.   
  49.      var val = obj.options[index].text;  
  50.   
  51. 7. 修改选项option  
  52.   
  53.      var obj=document.getElementById('mySelect');  
  54.   
  55.      var index=obj.selectedIndex; //序号,取当前选中选项的序号  
  56.   
  57.      var val = obj.options[index]=new Option("新文 本","新值");  
  58.   
  59. 8. 删除select  
  60.   
  61.       function removeSelect(){  
  62.             var mySelect = document.getElementById("mySelect");  
  63.             mySelect.parentNode.removeChild(mySelect);  
  64.      } 
阅读(1307) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~