IE 里 label 的 innerHTML 属性没用?我发现在 FIREFOX 里 label标签 的  innerHTML 属性可以用,但是 在IE 里(无论是IE几点0) 里 这个属性都没用,比如说:<label name='lb[]'>我要的文字</label>
<label name='lb[]'>我要的文字2222222</label>
lb=document.getElementsByName('lb');alert(lb[0].innerHTML);提示 lb.0.innerHTML 为空或不是对象

解决方案 »

  1.   

    纠正一下: lb=document.getElementsByName('lb[]');
      

  2.   

    lb=document.getElementsByName('lb');
    改成
    lb=document.getElementsByTagName('label');
    可以
      

  3.   

    可是我只要 lb[] 这个特定组的,万一有其他的 label 又不是这个组的怎么办?
      

  4.   

    用getElementsByTagName('label')来获取label元素吧。IE中getElementsByName()方法只对BUTTON, TEXTAREA, APPLET, SELECT, FORM, FRAME, IFRAME, IMG, A, INPUT, OBJECT, MAP, PARAM, META这些元素有效。并且,你贴出的代码也有问题,应该是:
    lb=document.getElementsByName('lb[]');
      

  5.   

    比如说这样:<label name='lb[]'>我要的文字</label>
    <label name='lb[]'>我要的文字2222222</label>
    <label name='xx[]'>我要的文字33333333</label>
    <label name='xx[]'>我要的文字4444444</label>
      

  6.   

    getElementsByName这个方法在ie中有问题,只能获取到几种标签的集合。或者你可以添加多个id和name重名,也能找到,但这种方式不推荐,兼容的话最好还是用getElementsByTagName吧。修改如下可以获取,但方法不推荐。<div name='lb' id="lb">我要的文字</div>
    <label name='lb' id="lb">我要的文字2222222</label>
      

  7.   


    遍历label对象集合,比较name值过滤(name并不是label元素的标准属性,将作为自定义属性对待):
    <label name='lb[]'>我要的文字</label>
    <label name='lb[]'>我要的文字2222222</label><label name='xx[]'>我要的文字33333333</label>
    <label name='xx[]'>我要的文字4444444</label><script type="text/javascript">
    lb=document.getElementsByTagName('label');
    var o = [];
    for (var i = 0; i < lb.length; i ++) if (lb[i].getAttribute('name') == 'lb[]') o.push(lb[i]);
    alert(o[0].innerHTML);
    </script>
      

  8.   

    只能用getElementsByTagName获取后再排除没有class="lb[]",name只有ie认得!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>label 的 innerHTML</title>
    </head>
    <body>
    <label class='lb[]'>我要的文字</label>
    <label class='lb[]'>我要的文字2222222</label><script type="text/javascript">
    lb=document.getElementsByTagName('label');
    var str= new Array();
    for(i=0;i<lb.length;i++)
    {
    if(lb[i].className=="lb[]")
    str[i]=lb[i].innerHTML;
    }
    alert(str[0]);
    </script>
    </body>
    </html>
      

  9.   


    可是这样写会不会效率不高,或者速度慢什么的,我奇怪的是为啥getElementsByName 在IE里有时候可以,有时候不行,我确认了都是同样的 chekbox 控件
      

  10.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test</title>
    </head>
    <script>
        var o=document.getElementsByTagName('input');
      
      alert(o.length);for (var i = 0; i < o.length; i ++)
    {
      if (o[i].getAttribute('name') == 'ckson[]') 
       {
          alert(o[i].value); 
       }
       
     }
     
     </script><body>
    <span id="result">马累北环礁 马累南环礁 </span><span id="iput">
    <input value="500" type="text" name="TopSelect[]" />
    <input value="1000" type="text" name="TopSelect[]" />
    </span><span id="iput2">
    <input id="500_Son" value="501,502," type="text" name="500_Son" />
    <input id="1000_Son" type="text" name="1000_Son" />
    </span>
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tbody>
        <tr>
          <td height="24" width="90"> 岛屿位置:</td>
          <td><input id="hidden_dywz" value="500" type="hidden" name="dywz" />
              <span id="span_dywz">
                <select id="dywz_top" name="dywz_top">
                  <option value="0">请选择..</option>
                  <option selected="selected" value="500">马累北环礁</option>
                  <option value="1000">马累南环礁</option>
                  <option value="1500">亚里北环礁</option>
                  <option value="2000">亚里南环礁</option>
                  <option value="2500">拉薇雅薇环礁</option>
                  <option value="3000">西努环礁</option>
                  <option value="3500">拉姆环礁</option>
                  <option value="4000">卡夫阿里夫环礁</option>
                  <option value="4500">塔环礁</option>
                  <option value="5000">诺鲁环礁</option>
                  <option value="5500">哈阿里夫环礁</option>
                  <option value="6000">美慕环礁</option>
                  <option value="6500">芭环礁</option>
                  <option value="7000">尼兰朵南环礁</option>
                  <option value="7500">尼兰朵北环礁</option>
                  <option value="8000">拉环礁</option>
                  <option value="8500">达鲁环礁</option>
                  <option value="9000">沙维亚尼环礁</option>
                </select>
            </span><span id="span_dywz_son"><span id="dywz_son" name="dywz_son">
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="501" type="checkbox" name="ckson[]" />
                  <label name="lb[]">波杜希蒂岛Bodu   Hithi</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="502" type="checkbox" name="ckson[]" />
                  <label name="lb[]">巴罗斯岛Baros</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="503" type="checkbox" name="ckson[]" />
                  <label name="lb[]">库达呼拉岛Kuda Huraa</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="504" type="checkbox" name="ckson[]" />
                  <label name="lb[]">满月岛fullmoon</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="505" type="checkbox" name="ckson[]" />
                  <label name="lb[]">天堂岛Paradise</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="506" type="checkbox" name="ckson[]" />
                  <label name="lb[]">索尼沃吉利岛soneva gili</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="507" type="checkbox" name="ckson[]" />
                  <label name="lb[]">阿雅达岛Ayada</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="508" type="checkbox" name="ckson[]" />
                  <label name="lb[]">梦幻岛Dhonveli</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="509" type="checkbox" name="ckson[]" />
                  <label name="lb[]">班度士岛Bandos</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="510" type="checkbox" name="ckson[]" />
                  <label name="lb[]">花芬岛Huvafen Fushi Spa Resort</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="511" type="checkbox" name="ckson[]" />
                  <label name="lb[]">艾丽湖岛Reef Ellaidhoo</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="512" type="checkbox" name="ckson[]" />
                  <label name="lb[]">美人蕉岛Thulhagiri</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="513" type="checkbox" name="ckson[]" />
                  <label name="lb[]">Meeru Island Resort蜜月岛(美禄岛)</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="514" type="checkbox" name="ckson[]" />
                  <label name="lb[]">伊呼噜岛Ihuru</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="515" type="checkbox" name="ckson[]" />
                  <label name="lb[]">白金岛Hudhuran</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="516" type="checkbox" name="ckson[]" />
                  <label name="lb[]">椰子岛Kurumba Maldives</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="517" type="checkbox" name="ckson[]" />
                  <label name="lb[]">哈林吉利岛Helengeli</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="518" type="checkbox" name="ckson[]" />
                  <label name="lb[]">夫瑞提拉岛Reethi Rah</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="519" type="checkbox" name="ckson[]" />
                  <label name="lb[]">泰姬珊瑚岛Vivanta Coral Reef Resort</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="520" type="checkbox" name="ckson[]" />
                  <label name="lb[]">夏日岛Summer Island</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="521" type="checkbox" name="ckson[]" />
                  <label name="lb[]">瓦宾法鲁岛vabbinfaru</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="522" type="checkbox" name="ckson[]" />
                  <label name="lb[]">卡尼岛Club Med Kani</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="523" type="checkbox" name="ckson[]" />
                  <label name="lb[]">法鲁岛 Faru</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="524" type="checkbox" name="ckson[]" />
                  <label name="lb[]">马累首都岛</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="525" type="checkbox" name="ckson[]" />
                  <label name="lb[]">马累国际机场岛</label>
                </span></span></td>
        </tr>
      </tbody>
    </table>
    </body>
    </html>我试过了,还是不行...好奇怪!!
      

  11.   

    将你的js代码放入window.onload里面,或者写在最下面。
    <script>
    window.onload=function(){
      var o=document.getElementsByTagName('input');
     
      alert(o.length);for (var i = 0; i < o.length; i ++)
    {
      if (o[i].getAttribute('name') == 'ckson[]') 
      {
      alert(o[i].value); 
      }
       
     }
     }
     
    </script>
      

  12.   

    我明白了,因为在HTML控件生成之前调用了某个函数,所以这个函数里找不到这些控件
      

  13.   

    但是奇怪的是虽然如果用 var ck=document.getElementsByName("ckson[]");
      
      alert(ck);又提示有这个 object HTMLCollection !!
    代码贴上了:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test</title>
    </head>
    <script>  var ck=document.getElementsByName("ckson[]");
      
      alert(ck);
      
      alert(ck.length); </script><body>
    <span id="result">马累北环礁 马累南环礁 </span><span id="iput">
    <input value="500" type="text" name="TopSelect[]" />
    <input value="1000" type="text" name="TopSelect[]" />
    </span><span id="iput2">
    <input id="500_Son" value="501,502," type="text" name="500_Son" />
    <input id="1000_Son" type="text" name="1000_Son" />
    </span>
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tbody>
        <tr>
          <td height="24" width="90"> 岛屿位置:</td>
          <td><input id="hidden_dywz" value="500" type="hidden" name="dywz" />
              <span id="span_dywz">
                <select id="dywz_top" name="dywz_top">
                  <option value="0">请选择..</option>
                  <option selected="selected" value="500">马累北环礁</option>
                  <option value="1000">马累南环礁</option>
                  <option value="1500">亚里北环礁</option>
                  <option value="2000">亚里南环礁</option>
                  <option value="2500">拉薇雅薇环礁</option>
                  <option value="3000">西努环礁</option>
                  <option value="3500">拉姆环礁</option>
                  <option value="4000">卡夫阿里夫环礁</option>
                  <option value="4500">塔环礁</option>
                  <option value="5000">诺鲁环礁</option>
                  <option value="5500">哈阿里夫环礁</option>
                  <option value="6000">美慕环礁</option>
                  <option value="6500">芭环礁</option>
                  <option value="7000">尼兰朵南环礁</option>
                  <option value="7500">尼兰朵北环礁</option>
                  <option value="8000">拉环礁</option>
                  <option value="8500">达鲁环礁</option>
                  <option value="9000">沙维亚尼环礁</option>
                </select>
            </span><span id="span_dywz_son"><span id="dywz_son" name="dywz_son">
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="501" type="checkbox" name="ckson[]"  />
                  <label name="lb[]">波杜希蒂岛Bodu   Hithi</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="502" type="checkbox" name="ckson[]"  />
                  <label name="lb[]">巴罗斯岛Baros</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="503" type="checkbox" name="ckson[]"  />
                  <label name="lb[]">库达呼拉岛Kuda Huraa</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="504" type="checkbox" name="ckson[]" />
                  <label name="lb[]">满月岛fullmoon</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="505" type="checkbox" name="ckson[]" />
                  <label name="lb[]">天堂岛Paradise</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="506" type="checkbox" name="ckson[]" />
                  <label name="lb[]">索尼沃吉利岛soneva gili</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="507" type="checkbox" name="ckson[]" />
                  <label name="lb[]">阿雅达岛Ayada</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="508" type="checkbox" name="ckson[]" />
                  <label name="lb[]">梦幻岛Dhonveli</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="509" type="checkbox" name="ckson[]" />
                  <label name="lb[]">班度士岛Bandos</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="510" type="checkbox" name="ckson[]" />
                  <label name="lb[]">花芬岛Huvafen Fushi Spa Resort</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="511" type="checkbox" name="ckson[]" />
                  <label name="lb[]">艾丽湖岛Reef Ellaidhoo</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="512" type="checkbox" name="ckson[]" />
                  <label name="lb[]">美人蕉岛Thulhagiri</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="513" type="checkbox" name="ckson[]" />
                  <label name="lb[]">Meeru Island Resort蜜月岛(美禄岛)</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="514" type="checkbox" name="ckson[]" />
                  <label name="lb[]">伊呼噜岛Ihuru</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="515" type="checkbox" name="ckson[]" />
                  <label name="lb[]">白金岛Hudhuran</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="516" type="checkbox" name="ckson[]" />
                  <label name="lb[]">椰子岛Kurumba Maldives</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="517" type="checkbox" name="ckson[]" />
                  <label name="lb[]">哈林吉利岛Helengeli</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="518" type="checkbox" name="ckson[]" />
                  <label name="lb[]">夫瑞提拉岛Reethi Rah</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="519" type="checkbox" name="ckson[]" />
                  <label name="lb[]">泰姬珊瑚岛Vivanta Coral Reef Resort</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="520" type="checkbox" name="ckson[]" />
                  <label name="lb[]">夏日岛Summer Island</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="521" type="checkbox" name="ckson[]" />
                  <label name="lb[]">瓦宾法鲁岛vabbinfaru</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="522" type="checkbox" name="ckson[]" />
                  <label name="lb[]">卡尼岛Club Med Kani</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="523" type="checkbox" name="ckson[]" />
                  <label name="lb[]">法鲁岛 Faru</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="524" type="checkbox" name="ckson[]" />
                  <label name="lb[]">马累首都岛</label>
                  <input onclick="setSon(this,'500_Son','马累北环礁');" value="525" type="checkbox" name="ckson[]" />
                  <label name="lb[]">马累国际机场岛</label>
                </span></span></td>
        </tr>
      </tbody>
    </table>
    </body>
    </html>