zoukankan      html  css  js  c++  java
  • 通过JavaScript操作HTML中select标签

    添加:

    Js代码 1.function selectChange()  

    2.{  

    3.var sel=document.getElementById("select1");  

    4. Option option = new Option("Text","Value");  

    5. sel.add(option);  

    6.}  

    function selectChange()

    {   var sel=document.getElementById("select1");  

         Option option = new Option("Text","Value");

         sel.add(option);

    }  

    删除所有:

    Js代码 1.document.getElementById("select1").options.length=0; 

    删除单个Option元素:

    Js代码 1.var sel=document.getElementById("select1");  

             2.for(i=0;i<sel.options.length;i++)  

             3.{  

             4. if(sel.options[i].selected)  

             5. {  

             6. sel.options[i]=null; //设置为null就删除了这个元素  

             7. }  

             8.}  

    var sel=document.getElementById("select1");

    for(i=0;i<sel.options.length;i++)  

    {    

        if(sel.options[i].selected)    

             {     sel.options[i]=null; //设置为null就删除了这个元素   }  

    }

    取值:

    Js代码 1.var sel=document.getElementById("select1"):  

             2.var val=sel.options[sel.selectedIndex].value  

             3.alert(val); //得到Option的value  

             4.var txt=sel.options[sel.selectedIndex].text  

             5.alert(txt); //得到Option的文本(即Text)  

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/JavaMan_KA/archive/2011/04/21/6339398.aspx

  • 相关阅读:
    分享图片到在线服务
    获取和保存照片
    处理图片(updated)
    简化版“询问用户是否退出”
    捕获高像素照片(updated)
    处理高像素的照片
    加强版照片捕获
    图片拍摄、处理、镜头应用
    Windows Phone 推送通知的第四类推送
    网络通信
  • 原文地址:https://www.cnblogs.com/xuxiuxiu/p/3290433.html
Copyright © 2011-2022 走看看