我有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);
是守失败的变量是LASTROW和nextCommentBox,但他们在函数的开始被定义。我现在还不能包裹我的头,这是为什么它可以在许多浏览器上运行,但IE无法找到变量?感谢您的帮助和建议。
编辑15年8月31日
我把IE控制台的一些截图显示的确切错误。如您所见,删除方法没有被调用,因为引用为空。在这种情况下,调用的引用是变量lastRow,它是一个表行元素。
检查我的代码,我们发现第一removeChild之函数被调用的lastRow.parentElement它确实存在:
lastRow.parentElement.removeChild(lastRow);
,做外的条件和console.logs在removeChild函数被调用之前,我们看到元素指针刚刚消失。
我换的innerHTML上表引用的的appendChild和removeChild之功能,但仍无法在IE浏览器。
'elementCounter'被用作从零开始的索引。但是你将它与一个基于的长度进行比较。当到达最后一个元素时,if条件仍然满足,并且调用具有未定义索引的checkCommenBox(超出边界)。这发生在所有浏览器中。 – Andreas