空格换行也是node,属于TextNode, 
childNodes.length =6不是指option里的text,而是<select>和<option>之间的文本以及<option>之间的文本
可以试一下去掉标签间的空格换行符,再运行看下
<select name="select" id="sel01"><option value="101">白菜101 </option><option value="102">青瓜102 </option><option value="203">狗狗203 </option></select>

解决方案 »

  1.   

    我又试了下,如果把其中的“青瓜102 ”去掉,即
       <p> 
         <select name="select" id="sel01"> 
           <option value="101">白菜101 </option> 
           <option value="102"></option> 
           <option value="203">狗狗203 </option> 
         </select>  
       </p> 结果,调用document.getElementById("sel01").childNodes.length返回值,还是6可是,用nodeName遍历所有("sel01")的子结点,除了option以外,都是#text,难道这个#text是别的东东??
      

  2.   

    1楼说的应该没错~~~~~~~~~~~~~~~~~~~
    你写成这样就没问题<select name="select" id="sel01"><option value="101">白菜101 </option><option value="102">青瓜102 </option><option value="203">狗狗203 </option></select> 
      

  3.   

    问题基本解决了,在上面的代码中,是因为回车符导致其中多了几个text元素,我就再钻钻牛角尖吧不过,我发现,在</option></select>之间的回车符没有被计算到。或者,换一个方法,如果代码改为<select name="select" id="sel01"> </select>   则用document.getElementById("sel01").childNodes.length返回为0,不知道谁能给个解释啊
      

  4.   

    <select name="select" id="sel01">     <option>a</option></select>就是<select name="select" id="sel01">     <option>中间的空格,不是<select name="select" id="sel01">  </select> 之间的空格.
      

  5.   

    #text就是文本元素。
    document.getElementById("sel01").childNodes.length返回子结点个数是没错,不过这里的“子”
    并不一定指“儿子”似的节点,而是指所有的节点,可能你理解错了。
      

  6.   

    补充一点,
    在CSS原本是块级的元素,IE下不会把空格作为文本节点,但FF,safer,opera都会,而原来行内元素之类的其他元素,使用display:block后,在IE下
    <select name="select" id="sel01">  <option value="101">白菜101  </option>  <option value="102"> </option>  <option value="203">狗狗203  </option>  </select> 
    得到6个节点.FF,safer为7个节点,opera为3  
      

  7.   

    IE下的childNodes包括textNode和HTMLElement 
    IE下你可以使用children属性返回直接子HTMLElement集合
    FF下的childNodes没有这个问题