为什么此Javascript DOM代码只能使用FF,而不能使用IE? [英] Why does this Javascript DOM code only working FF, but not IE?

查看:45
本文介绍了为什么此Javascript DOM代码只能使用FF,而不能使用IE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//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;
}

IE DOM分析器中的 children属性是否可用?

Is the "children" property available in the IE DOM Parser?

推荐答案

在IE中,XML文档无法实现与HTML文档相同的文档对象模型;特别是XML Node 对象没有 儿童 属性,这是非标准属性。

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.

您应使用 childNodes 集合代替。但是请注意,在Firefox和其他浏览器(以及IIRC,在IE中非常特定的情况下)中,此集合还将包括仅包含空格的文本节点,例如原始XML文件中的换行符。因此,您需要检查 nodeType 属性:如果其值为1,则它是一个元素,并且将具有诸如 tagName 之类的属性。

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.

此外,随着MSXML实现 DOM级别1 ,而Firefox实施 DOM级别3 ,您将无法使用级别3中引入的 textContent 属性。相反,您必须遍历 nodeType childNodes === 3并连接其 nodeValue 属性,然后可能会想要修剪任何内容前导或尾随空格。或者,如果您知道那里永远只有textNode,请调用 规范化元素的方法 ,以确保它只有一个文本节点子元素。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆