为什么这个Javascript DOM代码只能在FF中工作,而不是IE? [英] Why does this Javascript DOM code only workin FF, but not IE?
问题描述
//创建一个XML解析器的实例
if(window.ActiveXObject)
{
//检查浏览器是否为IE
xmlDoc = new ActiveXObject(Microsoft.XMLDOM);
xmlDoc.async =false; //确保文件完全加载
xmlDoc.load(strPath)//在解析器
中加载文件if(xmlDoc.parseError.errorCode!= 0)
{
alert(Error#:+ xmlDoc.parseError.errorCode;
}
}
// for mozilla based browsers
else if(document.implementation& & document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument(,doc,null);
xmlDoc.async = false; //确保文档被加载
loaded = xmlDoc.load(strPath);
if(!loaded)
{
alert(XML文件中的错误);
}
}
//解析XML
var root = xmlDoc.documentElement;
level1Nodes = root.children;
for(var index1 = 0; index1 < level1Nodes.length; index1 ++)
{
//从XML中提取标记内容
var level1Node = level1Nodes [index1];
var strName = level1Node.children [0] .textContent;
var strHeader1 = level1Node.children [1] .tagName;
var strHeader1Content = level1Node.children [1] .textContent;
}
任何想法? IE DOM解析器中的children属性是否可用?
提前感谢所有的帮助!
,XML文档不能实现与HTML文档相同的文档对象模型;特别是XML Node
对象没有 children
属性,这是非标准的。
你应该使用 childNodes
集合代替。但是请注意,在Firefox和其他浏览器以及IIRC中,在IE的非常具体的情况下,此集合还将包含仅包含空格的文本节点,例如原始XML文件中的换行符。所以你需要检查 nodeType
property:如果它的值为1,它是一个Element,并且将具有诸如 tagName
的属性。
此外,由于MSXML实现 DOM 1级,而Firefox实现 DOM Level 3 ,您将无法使用第3级中引入的 textContent
属性,而是必须遍历 childNodes
nodeType
=== 3并连接他们的 nodeValue
属性,然后可能会修剪任何领先或尾随的空白。或者,如果你知道那里只有文本节点,请调用 normalize
方法,以确保它只有一个文本节点子节点。
<没有人说这个东西应该很简单: - (
//create an instance of the XML parser
if (window.ActiveXObject)
{
//Checking if the browser is IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false"; //make sure doc is fully loaded
xmlDoc.load(strPath) //load the file in the parser
if (xmlDoc.parseError.errorCode != 0)
{
alert("Error #: " + xmlDoc.parseError.errorCode;
}
}
//for mozilla based browsers
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","doc",null);
xmlDoc.async=false; //make sure doc is fully loaded
loaded = xmlDoc.load(strPath);
if(!loaded)
{
alert("Error in XML File");
}
}
//Parse the XML
var root = xmlDoc.documentElement;
level1Nodes = root.children;
for(var index1 = 0; index1 < level1Nodes.length; index1++)
{
//Extract the markup content from XML
var level1Node = level1Nodes[index1];
var strName = level1Node.children[0].textContent;
var strHeader1 = level1Node.children[1].tagName;
var strHeader1Content = level1Node.children[1].textContent;
}
Any ideas? Is the "children" property available in the IE DOM Parser?
Thanks in advance for all your help!
In IE, an XML document does not implement the same document object model as an HTML document; in particular, XML Node
objects don't have the children
property, which is non-standard.
You should use the childNodes
collection instead. However be aware that in Firefox and other browsers - and, IIRC, under very specific circumstances in IE - this collection will also include text nodes that contain only whitespace, such as line breaks in the original XML file. So you will need to check the nodeType
property: if it has the value 1, it is an Element, and will have properties such as tagName
.
Furthermore, as MSXML implements DOM Level 1, whereas Firefox implements DOM Level 3, you won't be able to use the textContent
property, which was introduced in Level 3. Instead, you will have to iterate over the childNodes
of nodeType
=== 3 and concatenate their nodeValue
properties, and probably then will want to trim any leading or trailing whitespace. Alternatively, if you know that there will only ever be textNodes in there, call the normalize
method of the element first to make sure it only has one text node child.
Nobody ever said this stuff was supposed to be easy :-(
这篇关于为什么这个Javascript DOM代码只能在FF中工作,而不是IE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!