我写了一个网页(包含HTML,CSS和javascript),它将XML文件中的信息加载到"填写空白"在页面上的元素。除非我在Internet Explorer中加载页面(我使用IE 11.0),否则一切都很完美。
我遇到了一些问题并解决了其中的一些问题,但新问题不断涌现,所以我想我不了解IE如何处理XML。
我得到的错误是:
Unable to get property 'querySelectorAll' of undefined or null reference
当我使用代码时:
selectedBookNodeList = bookDataDocument.getElementById(bookId);
在IE调试器中它显示" selectedBookNodeList"为NULL,但bookDataDocument不为null。同样,这在Chrome和Firefox中完美运行(这里是一个链接:http://www.pattmayne.com/stories2/)。
为什么IE11中它为NULL,但在其他浏览器中工作正常?
以下是所有代码:
var bookPage;
var bookDataDocument;
var XMLConnection;
var bookDataRootNode;
var selectedBookNode;
var bookDescription;
var bookDescription2;
var bookDescription3;
var bookImage;
var bookTitle;
var paperbackLink;
var ebookLink;
function setupXML()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
XMLConnection=new XMLHttpRequest();
}
else
{// code for IE6, IE5
XMLConnection=new ActiveXObject("Microsoft.XMLHTTP");
}
XMLConnection.open("GET", "novels/book_data.xml", false);
XMLConnection.send(null);
bookDataDocument = XMLConnection.responseXML;
}
function setBook(bookId)
{
selectedBookNodeList = bookDataDocument.getElementById(bookId);
bookTitle = selectedBookNodeList.querySelectorAll("title").item(0).childNodes[0].nodeValue;
bookDescription = selectedBookNodeList.querySelectorAll("description").item(0).childNodes[0].nodeValue;
bookDescription2 = selectedBookNodeList.querySelectorAll("description2").item(0).childNodes[0].nodeValue;
bookDescription3 = selectedBookNodeList.querySelectorAll("description3").item(0).childNodes[0].nodeValue;
ebookLink = selectedBookNodeList.querySelectorAll("ebookLink").item(0).childNodes[0].nodeValue;
paperbackLink = selectedBookNodeList.querySelectorAll("paperbackLink").item(0).childNodes[0].nodeValue;
bookImage = selectedBookNodeList.querySelectorAll("coverImage").item(0).childNodes[0].nodeValue;
bookPage = document.getElementById("storiesWindow").contentWindow.document;
bookPage.getElementById("bookTitle").innerHTML = bookTitle;
bookPage.getElementById("description").innerHTML = bookDescription;
bookPage.getElementById("description2").innerHTML = bookDescription2;
bookPage.getElementById("description3").innerHTML = bookDescription3;
bookPage.getElementById("buyEbookLink").href = ebookLink;
bookPage.getElementById("buyPaperbackLink").href = paperbackLink;
bookPage.getElementById("coverImage").src=bookImage;
}
我真的很喜欢解决方案,但我更喜欢理解我明显不理解的动态。我想将来使用XML作为其他东西。
答案 0 :(得分:0)
我通过完全跳过“getElementById”语句解决了这个问题。相反,我查询了“book”然后使用了“getAttribute('id')”并编写了一个带循环的函数来比较id和发送到原始函数的bookId。
这是固定的,有效的代码:
function setBook(bookId)
{
listOfBooks = bookDataDocument.querySelectorAll("book");
selectedBook = listOfBooks.item(2);
getSelectedBook(bookId);
bookTitle = selectedBook.querySelectorAll("title").item(0).childNodes[0].nodeValue;
bookDescription = selectedBook.querySelectorAll("description").item(0).childNodes[0].nodeValue;
bookDescription2 = selectedBook.querySelectorAll("description2").item(0).childNodes[0].nodeValue;
bookDescription3 = selectedBook.querySelectorAll("description3").item(0).childNodes[0].nodeValue;
ebookLink = selectedBook.querySelectorAll("ebookLink").item(0).childNodes[0].nodeValue;
paperbackLink = selectedBook.querySelectorAll("paperbackLink").item(0).childNodes[0].nodeValue;
bookImage = selectedBook.querySelectorAll("coverImage").item(0).childNodes[0].nodeValue;
bookPage = document.getElementById("storiesWindow").contentWindow.document;
bookPage.getElementById("bookTitle").innerHTML = bookTitle;
bookPage.getElementById("description").innerHTML = bookDescription;
bookPage.getElementById("description2").innerHTML = bookDescription2;
bookPage.getElementById("description3").innerHTML = bookDescription3;
bookPage.getElementById("buyEbookLink").href = ebookLink;
bookPage.getElementById("buyPaperbackLink").href = paperbackLink;
bookPage.getElementById("coverImage").src=bookImage;
}
function getSelectedBook(bookId)
{
for (var i=0; i<listOfBooks.length; i++)
{
bookString = listOfBooks.item(i).getAttribute("id");
if (bookString == bookId)
selectedBook = listOfBooks.item(i);
}
}
function setupXML()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
XMLConnection=new XMLHttpRequest();
}
else
{// code for IE6, IE5
XMLConnection=new ActiveXObject("Microsoft.XMLHTTP");
}
XMLConnection.open("GET", "novels/book_data.xml", false);
XMLConnection.send(null);
bookDataDocument = XMLConnection.responseXML;
}
答案 1 :(得分:0)
嗨,我知道这是一个旧的,但我昨天遇到了一个与客户相同的问题。我在这个案例中发现的问题是客户端有IE11和Flash 11,但我的PC有IE11和Flash 14;我更新了他的闪存,一切正常,看起来像Flash 11是IE11之前的版本,在DOM中对flash对象有一些支持问题。
希望这也有助于其他人: - )
此致
利安