【问题标题】:Live nodes from querySelectorAll来自 querySelectorAll 的活动节点
【发布时间】:2014-02-14 21:41:24
【问题描述】:

我有一个在 Firefox 中运行的脚本,它使用通过 getElementsByClassName 提取的元素数组。 IE8 不支持此方法,因此我必须替换 querySelectorAll。我的问题是 querySelectorAll 创建了一个静态列表,而不是对实际元素的实时引用。

我的脚本

function subMenu(sCat,gCat,sh,gh,selection)
{
    sElements = document.querySelectorAll('cat'+sCat);
    gElements = document.querySelectorAll('cat'+gCat);

//  sElements = document.getElementsByClassName('cat'+sCat);
//  gElements = document.getElementsByClassName('cat'+gCat);

if(sh>0)
    {
    for(i=0;i<sElements.length;i++)
        {
        if(!(h = window.getComputedStyle(sElements[i],null).height)) {h=sElements[i].currentStyle;}
        nh = parseInt(h.replace("px",""))-4;
        sElements[i].style.height = nh+"px";
        }
    sh=sh-4;
    }
if(gh<100)
    {
    for(i=0;i<gElements.length;i++)
        {
        if(!(h = window.getComputedStyle(gElements[i],null).height)) {h=gElements[i].currentStyle;}
        nh = parseInt(h.replace("px",""))+4;
        gElements[i].style.height = nh+"px";
        }
    gh=gh+4;
    }
if(sh>0 || gh<100) {xMenu=setTimeout("subMenu("+sCat+","+gCat+","+sh+","+gh+",'"+selection+"')",10);}
else
    {
    fadeOut(selection);
    }
}

我已经注释掉了可以正常工作但在 IE8 中不行的脚本。我不确定 getComputedStyle 或 currentStyle 属性是否有效,但设置 style.height 属性肯定不行。

所以我的问题:

  1. 有没有办法从 IE8 中获取类似于 getElementsByClassName 的活动节点列表?

  2. 谁能建议一种将数组从 querySelectorAll 转换为元素对象数组的方法?

【问题讨论】:

    标签: javascript internet-explorer-8


    【解决方案1】:

    querySelectorAll 顾名思义需要一个选择器。类以点开头,就像在 CSS 中一样:

    sElements = document.querySelectorAll('.cat'+sCat);
                                          -^-
    

    将其转换为真正的数组:

    realArray = Array.prototype.slice.call(pseudoArray);
    

    【讨论】:

    • 他支持 IE8,但是这个 Array 技巧在 IE8 中不起作用。
    • 谢谢。这绝对解决了 querySelectorAll 问题。 ie 仍然存在问题,但那是我的问题。猜猜“静态”只是指元素列表,而不是它们的属性。
    猜你喜欢
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 2015-05-19
    相关资源
    最近更新 更多