This question already has answers here:
getElementsByClassName() doesn't work in old Internet Explorers like IE6, IE7, IE8
                                
                                    (5个答案)
                                
                        
                                5年前关闭。
            
                    
样本来源:

 <div>
            <br />
         <a name="IDATLQHE"></a>
        <h2 class="subhead" xmlns="">
         <div class="dummy">
             <div class="dummyy">
                <span>abcd</span>
            </div>

            <div class="dummyyy">
                <span>
                        <a title="google" href="http://google.com">google.com</a>
                </span>
          </div>
        </div>
        </h2>
    </div>


需要的输出:

我想用<a>元素的内容替换<div class="dummyyy">元素中<div class="dummyy">元素的内容。

编写的JavaScript:

<script type="text/javascript">
    var divs = document.getElementsByClassName('dummy');
    for(var i = 0, len = divs.length; i < len; i++)
    {
        var div = divs[i],
        h2 = div.getElementsByClassName('dummyyy'),
        h3 = div.getElementsByClassName('dummyy');
        if(h2.length === 1)
        {
            var aa= h2[0],
            aaa=aa.getElementsByTagName('a');
            if(h2.length === 1 && h3.length === 1)
            {
                aaa[0].innerHTML = h3[0].innerHTML;
                h3[0].innerHTML=null;
            }
        }
    }


该脚本在Firefox中可以正常工作,但在IE中则不能。

错误:

 getElementsByClassName
 aaa=aa.getElementsByTagName('a')


谁能帮上忙。

谢谢

最佳答案

IE8不支持getElementsByClassName。如果不需要IE7支持,则可以将其替换为div.querySelectorAll(".dummyy")

08-15 16:43
查看更多