当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM Mozilla 和 IE

XML DOM
DOM 节点
DOM 节点列表
DOM 解析
DOM 遍历节点树
DOM Mozilla 和 IE
DOM 获取节点
DOM 设置节点
DOM 删除节点
DOM 更换节点
DOM 建立节点
DOM 添加节点
DOM 克隆节点
DOM 节点类型
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentType
DOM ProcessingInstr
DOM Element

XML DOM 中的 DOM Mozilla 和 IE


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 894 ::
收藏到网摘: n/a

Browser-differences in DOM Parsing
不同浏览器对DOM解析的不同

Both Mozilla and Internet Explorer supports W3C's DOM specification.
Mozilla 和 IE 都支持W3C's DOM 规范。

However, there are still differences between Internet Explorer's DOM and Mozilla's DOM. The most important difference is how they handle white-space text nodes. When XML generates, it often contains white-spaces between the nodes. Internet Explorer, when using node.childNodes[], will NOT contain these white-space nodes. In Mozilla, those nodes will be in the array.
然而,IE 和 Mozilla 对 DOM 的处理方式是不同的,最大的区别就是在空格文本节点处理上的差异。当生成XML时,通常会在节点之间包含空格。在IE中,我们可以使用“node.childNodes[]”去掉这些空格;在Mozilla中,这些节点将存在于数组中。

请看看下面的这个XML文件:books.xml

The following code fragment alerts how many child nodes the root element have:
下述代码片断将会提示根元素中包含的子节点数量:

xmlDoc.load("books.xml");
var x=xmlDoc.documentElement.childNodes;
alert(x.length)
for (i=0;i<x.length;i++)
{
document.write(x[i].nodeType);
document.write("<br />");
}

自己尝试一下吧

Internet Explorer will skip the white-space text nodes that are generated between nodes (e.g. new line characters), while Mozilla will not. So, in the example above, Mozilla browsers will alert 9 child nodes, while Internet Explorer will alert 4.
IE 将会跳过节点之间生成的空格文本节点(例如:新行符);然而,Mozilla 并不会这样做。在上述案例中,Mozilla 浏览器会提示9个子节点,但是IE只会提示4个。

To iterate through the child nodes and disregard those text nodes, you can check the node type. An element node has type 1, a text node has type 3, and a comment node has type 8. To skip text nodes, you can process only nodes that are not of node type 3 (text nodes) and node type 8 (comment nodes):
通过对子节点的重申并忽略这些文本节点,你可以查阅节点类型。元素节点为“类型1”;文本节点为“类型3”;注释节点为“类型8”。你可以仅通过处理非文本节点(类型3)和注释节点(类型8)的节点来跳过对文本节点的处理,具体如下:

xmlDoc.load("books.xml");
var x=xmlDoc.documentElement.childNodes;
for (var i=0;i<x.length;i++)
{
if ((x[i].nodeType!=3)&&(x[i].nodeType!=8))
{
//Do not process text nodes or comment nodes
document.write(x[i].nodeName);
document.write("<br />");
}
}

自己尝试一下吧

The best way to only process element nodes is to iterate through the child nodes and only process those with a node type of 1:
仅对元素节点进行处理的最好方法就是通过重申子节点并仅对“类型1”的节点进行处理,具体如下:

xmlDoc.load("books.xml");
var x=xmlDoc.documentElement.childNodes;
for (var i=0;i<x.length;i++)
{
if (x[i].nodeType==1)
{
//Process only element nodes
document.write(x[i].nodeName);
document.write("<br />");
}
}

如果你想学习完整的节点类型参数以及它们的子节点参数,可以查阅我们的节点类型参数

评论 (0) All

登陆 | 还没注册?