2014-01-24 24 views
1

我有一個在Firefox中工作的腳本,它使用一個用getElementsByClassName提取的元素數組。 IE8不支持此方法,因此我必須替換querySelectorAll。我的問題是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屬性defintiely不是。

所以我的問題:

  1. 有沒有一種方式來獲得IE8類似getElementsByClassName方法活節點列表?

  2. 任何人都可以提出一種將數組從querySelectorAll轉換爲元素對象數組的方法嗎?

回答

1

querySelectorAll顧名思義期望選擇器。類以點開始,就像在CSS:

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

要將其轉換爲真正的數組:

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

他支持IE8,但陣招不IE8工作。 – rvighne

+0

謝謝。這絕對解決了querySelectorAll問題。仍有問題,即進一步下降,但這是我的問題。猜'靜態'只是指元素列表,而不是它們的屬性。 – illusivetech