输出所有select元素下的所有option元素中对应的文本内容

 输出所有select元素下的所有option元素中对应的文本内容, 例如:<option value="中专">中专^^</option>  输出--->中专^^


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>yuhan20081021</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
  <select name="edu" id="edu">
   <option value="博士">博士^^^^^</option>
 <option value="硕士">硕士^^^^^</option>
 <option value="本科" selected="selected" >本科^^^^^</option>
 <option value="幼儿园">幼儿园^^^^^</option>
  </select> 
  </body>
  
  【法一】<script language="JavaScript">
    //获取select元素的引用
var selectElement=document.getElementById("edu");
    //输出所有select元素下的所有option元素中对应的文本内容
         //例如:<option value="中专">中专^^</option>  输出--->中专^^
    var optionElements=selectElement.getElementsByTagName("option");
    for(var i=0;i<optionElements.length;i++){
      alert(optionElements[i].firstChild.nodeValue);
}
  </script>

【法二】

 <script language="JavaScript">
     //增加节点<option value="大专">大专^^^^^</option>
     var optionElement=document.createElement("option");
optionElement.setAttribute("value","大专");
     var textElement=document.createTextNode("大专^^^^^");   
     optionElement.appendChild(textElement);
var eduElement=document.getElementById("edu");
eduElement.appendChild(optionElement);   
  </script>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值