我有一个在 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 属性肯定无效。
所以我的问题:
是否有类似 getElementsByClassName 的方法可以从 IE8 获取事件节点列表?
任何人都可以建议一种将数组从 querySelectorAll 转换为元素对象数组的方法吗?
最佳答案
querySelectorAll
顾名思义,需要一个选择器。类以点开头,就像在 CSS 中一样:
sElements = document.querySelectorAll('.cat'+sCat);
-^-
将其转换为真正的数组:
realArray = Array.prototype.slice.call(pseudoArray);
关于javascript - 来自 querySelectorAll 的事件节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323466/