2011-07-04 116 views
0

我有這樣的代碼:for循環,這並不需要運行

<script type="text/javascript"> 
var url = "http://www.xxxxx.xxx/xxxxxxxxx"; 

var txt; 
var id1; 
var id2; 
var imgarres = []; 
var imgarr = []; 
var imgels = []; 

function getdata() { 
    if (id1){clearTimeout(id1);} 
    if (id2){clearTimeout(id2);} 

    var xhr = new XMLHttpRequest(); 
    xhr.open('GET',url, true); 
    xhr.setRequestHeader('Cache-Control', 'no-cache'); 
    xhr.setRequestHeader('Pragma', 'no-cache'); 

    xhr.onreadystatechange = function() { 
     if (xhr.readyState == 4) { 
      txt = xhr.responseText; 

      var r = txt.indexOf('<b class="fl_r">Online</b>'); 
      var el = document.createElement("div"); 

      el.innerHTML = txt; 

      var n = imgprocess(el);  
      var nam = el.getElementsByTagName("title")[0].innerHTML; 

      if (r != -1) { 
       var notification = webkitNotifications.createNotification('plus.gif', nam, 'online!!'); 
       notification.show(); 
       var id1 = setTimeout(getdata, 60000); 
      } else { 
       var notification = webkitNotifications.createNotification(n, nam, 'offline!!'); 
       notification.show(); 
       var id2 = setTimeout(getdata, 600000); 
      } 
     } 
    } 

    xhr.send();  
} 

function imgprocess(text) { 
    imgels = text.getElementsByTagName("IMG"); 
    for (var i=0;i< imgels.length;i++) { 
     if (imgels[i].src.indexOf(parse(url)) != -1) { 
      imgarr = imgels[i]; 
     } 
    } 

    for (var p=0; p< imgarr.length; p++) { 
     if (imgarr[p].parentNode.nodeName=="A") { 
      imgarres = imgarr[p]; 
     } 
    } 

    var z = imgarres[0].src; 
    return z; 
} 

function init() { 
    getdata(); 
} 
</script> 
</head> 
<body onload="init();"> 

當我執行這個代碼,錯誤說:「SRC無法讀取的未定義」關於var z = imgarres[0].src;當我從那條線,擴建工程中刪除src沒有錯誤,但 imgprocess例程不會返回期望值!預期值是imgurl,這是我刪除的src。看起來第二個循環(for (var p=0; p< imgarr.length; p++){)根本沒有運行,但第一個是OK。我該如何解決?

P.S .:我試着像這樣傳遞迴調:xhr.onreadystatechange = function(imgprocess) { 但它不起作用。它表示「未捕獲的typeerror」對象不是函數。

回答

3
if (imgels[i].src.indexOf(parse(url)) != -1){ 
    imgarr = imgels[i]; 
} 

它看起來像你在上面的代碼中用單個元素覆蓋數組。

添加編輯:

如果if條件爲真,imgels[i]是元件(具有src),但代替添加imgels [I]到imgarr陣列,要更改imgarr爲指向單個元件。

然後在第二個for循環中,您將它視爲一個數組。

事實上,這在第二個循環中也是一個錯誤。 imgarres應該是一個數組或不是?如果是,那麼imgarres = imgarr[p];是錯誤的(它指向一個元素後,你這樣做)。如果不是那麼var z = imgarres[0].src;是錯誤的(如果它是一個元素,你不需要[0])。

添加編輯:

somearray = someelement; 

元素不添加到陣列中!

somearray.push(someelement); 

確實。

添加編輯:只是試試這個。誰知道,它可能會工作...

function imgprocess(text){ 
// get all IMG elements below the div 
imgels = text.getElementsByTagName("IMG"); 
// filter them somehow 
imgarr = []; 
for (var i=0;i< imgels.length;i++){ 
    if (imgels[i].src.indexOf(parse(url)) != -1){ 
    imgarr.push(imgels[i]); 
    } 
} 

// filter again, could probably be joined into one loop 
imgarres = []; 
for (var p=0; p< imgarr.length; p++){ 
    if (imgarr[p].parentNode.nodeName=="A"){ 
    imgarres.push(imgarr[p]); 
    } 
} 
// return the first image's src if any 
if (imgarres.length > 0) { 
    return imgarres[0].src; 
} 
return null; 
} 
+0

你是什麼意思?我首先檢查與console.log(imgarr)循環;第一個for循環很好.. – DrStrangeLove

+0

我添加了一些解釋並解釋了爲什麼第二個循環是錯誤的。 – lmz

+0

那麼爲什麼console.log(imgarr)輸出我需要的img元素? – DrStrangeLove