2015-08-28 24 views
0

我有JavaScript代码,似乎在Chrome,Firefox和Safari的工作,但无法在IE中工作,由于一些变量是“未定义”,但如果你看我的代码你会看到变量被正确赋值。我觉得它与它的定义范围有关,但我不想在尝试修复IE时破坏其他浏览器的代码。这个问题发生在IE11和IE8中,但我没有测试其他版本的IE来验证。Js变量未定义在IE后定义

function resizeCommentBox(obj,i){ 
    var lastItemCounter = obj[i].getElementsByTagName('tr').length - 1; 
    var lastRow = obj[i].getElementsByTagName('tr')[lastItemCounter]; 
    var nextCommentBox = obj[i+1].getElementsByTagName('table')[0]; 

    console.log("lastRow.parentElement right outside the first conditional if(obj[i].offsetHeight < obj[i].scrollHeight): "); 
    console.log(lastRow.parentElement); 

    if(obj[i].offsetHeight < obj[i].scrollHeight){ 
     if(!(i+2 < obj.length)){ 
      var template = document.getElementById('lastPageTemplate'); 
      var parentNode = template.parentNode; 

      template.style.display="block"; 
      template.removeAttribute("id"); 

      parentNode.innerHTML = parentNode.innerHTML + template.outerHTML; 
      parentNode.children[parentNode.children.length-1].setAttribute('id','lastPageTemplate'); 
      parentNode.children[parentNode.children.length-1].style.display="none"; 
      parentNode.children[parentNode.children.length-1].innerHTML = parentNode.children[parentNode.children.length-1].innerHTML; 

     } 

     console.log("lastRow.parentElement right outside the conditional if(lastRow.childElementCount > 0): "); 
     console.log(lastRow.parentElement); 
     if(lastRow.childElementCount > 0){ 
      nextCommentBox.innerHTML = lastRow.outerHTML + nextCommentBox.innerHTML; 
      lastRow.parentElement.removeChild(lastRow); 
     } 
     else{ 
      lastRow.parentElement.removeChild(lastRow); 
     } 

      if(nextCommentBox.getElementsByTagName('tr')[nextCommentBox.getElementsByTagName('tr').length-1].innerHTML === ""){ 
       nextCommentBox.getElementsByTagName('tr')[nextCommentBox.getElementsByTagName('tr').length-1].parentElement.removeChild(nextCommentBox.getElementsByTagName('tr')[nextCommentBox.getElementsByTagName('tr').length-1]); 
      } 


     resizeCommentBox(obj,i); 
    } 
    else{ 
     try{ 
      var lastPageOnlyFooter = document.getElementsByClassName('keepOnLastPageOnly'); 
      for(var j = 0; j < lastPageOnlyFooter.length - 2; j++){ 
       lastPageOnlyFooter[j].parentElement.removeChild(lastPageOnlyFooter[j]); 
      } 
     } 
     catch(err){ 
      console.log(err); 
     } 
     return false; 
    } 
} 

function checkCommenBox(elementCounter){ 

    var commentBoxCollection = document.getElementsByClassName('comments'); 
    resizeCommentBox(commentBoxCollection,elementCounter); 
    commentBoxCollection[elementCounter].style.height = "auto"; 

    if(elementCounter < commentBoxCollection.length){ 
     checkCommenBox(elementCounter + 1); 
    } 

} 

checkCommenBox(0); 

是守失败的变量是LASTROWnextCommentBox,但他们在函数的开始被定义。我现在还不能包裹我的头,这是为什么它可以在许多浏览器上运行,但IE无法找到变量?感谢您的帮助和建议。

编辑15年8月31日
我把IE控制台的一些截图显示的确切错误。如您所见,删除方法没有被调用,因为引用为空。在这种情况下,调用的引用是变量lastRow,它是一个表行元素。

IE11 javascript console error

检查我的代码,我们发现第一removeChild之函数被调用的lastRow.parentElement它确实存在:

lastRow.parentElement.removeChild(lastRow); 

,做外的条件和console.logs在removeChild函数被调用之前,我们看到元素指针刚刚消失。

console.logs

我换的innerHTML上表引用的的appendChildremoveChild之功能,但仍无法在IE浏览器。

+0

'elementCounter'被用作从零开始的索引。但是你将它与一个基于的长度进行比较。当到达最后一个元素时,if条件仍然满足,并且调用具有未定义索引的checkCommenBox(超出边界)。这发生在所有浏览器中。 – Andreas

回答

0

我通过脚本完成了几个日志到控制台中,发现了这个问题。看来,下面一行由可变LASTROW空:

parentNode.innerHTML = parentNode.innerHTML + template.outerHTML; 

前面提到的前行,LASTROW有一个元素对象的值。该行执行后,lastRow值将变为空(仅在IE上)。为了解决这个问题,我必须更改parentNode.innerHTML以使用appendChild on template.cloneNode(true)

parentNode.appendChild(template.cloneNode(true)); 

然后我移动了一些其他功能,使我的代码像以前一样工作。

3

我不知道的你想在这里做什么,但使用的innerHTML和outerHTML在IE浏览器属性是不建议,因为它是唯一的“差不多”的支持,特别是具有内表。请参阅http://www.quirksmode.org/dom/html/并尝试以此方式进行挖掘。