当前位置:编程学习 > JS >>

javascript常用方法、属性集合及NodeList 和 HTMLCollection 的浏览器差异

答案:
在您开始本文的阅读前,我强烈建议您可以先读一读此篇:http://w3help.org/zh-cn/causes/SD9004.

HTMLCollection 接口定义

inte易做图ce HTMLCollection
{
readonly attribute unsigned long length;
Node item(in unsigned long index);
Node namedItem(in DOMString name);
}


对于 HTMLCollection集合对象 必须要说一说的是 namedItem方法. 看看规范的解释.
原文:
namedItem method
This method retrieves a Node using a name. With [HTML 4.01] documents, it first searches for a Node with a matching id attribute. If it doesn't find one, it then searches for a Node with a matching name attribute, but only on those elements that are allowed a name attribute. With [XHTML 1.0] documents, this method only searches for Nodes with a matching id attribute. This method is case insensitive in HTML documents and case sensitive in XHTML documents.

翻译:
namedItem 方法:
此方法获通过 "name"属性来获取节点.
在HTML4.01文档中,它首先搜索的是节点的ID属性的值. 如果没找到匹配节点,才去搜索name 属性与之匹配的节点. 即HTML4.01 DTD下,浏览器们应该优先通过ID来获取节点.其次才是name.
在XHTML 1.0文档中,则仅搜索ID与之匹配的节点.
对于节点(id or name)属性的值,此方法在HTML文档中忽略大小写区别,而在XHTML文档中.则要区别大小写.

上文中粗体部分很重要,没有这个作为指导的话.后面遇到的一些问题就很不好确定孰是孰非.因为众多浏览器的实现并不一样.


NodeList 接口定义

inte易做图ce NodeList {
Node
item(in unsigned long index);
readonly attribute unsigned long
length;
};


微软MSDN上查到的 NodeList实现 ,虽然这些资料告诉我们 NodeList继承了 Microsoft.SpeechServer.Dom.Collections.Collection Class . 但是事实却并不如此. 事实上,ie浏览器的NodeList不具备 ICollection接口定义的 namedItem 和 tags 两个方法. 实现了他们的 只有HTMLCollection类型.
此文档是 Speech Server 2007 用的,所以应该仅供参考.只能说明IE浏览器中的NodeList 还是遵守标准的.
public sealed class NodeList : Collection, INodeList, IEnumerable, IExpando, IReflect

NodeList的继承链:
System.Object
  Microsoft.SpeechServer.Dom.Shim
    Microsoft.SpeechServer.Dom.DynamicShim
      Microsoft.SpeechServer.Dom.Collections.Collection
      Microsoft.SpeechServer.Dom.Collections.NodeList


Collection 实现的ICollection接口定义的属性和方法表

public properties : item(msdn上说item是重载,我表示诧异...),length
public methods : item,namedItem,tags

ps:
1. 目前只有Opera的NodeList Class 是派生自 Collection Class 或HtmlCollection Class 的.所以此浏览器中NodeList集合对象也会具备 HTMLCollection接口实现的所有属性和方法.
2. MS 的ICollection 接口 定义了一个tags方法 用来根据tagName获取元素集合.其类型为 HTMLCollection 类型




神秘的 StaticNodeList

inte易做图ce NodeSelector {
Element querySelector(in DOMString selectors);
NodeList querySelectorAll(in DOMString selectors);
}

The NodeList object returned by the querySelectorAll() method must be static, not live ([DOM-LEVEL-3-CORE], section 1.1.1)


由于w3.org的[DOM-LEVEL-3-CORE]文档中,并没有StaticNodeList接口的定义. 只好在后面找出一份微软的代替之.


微软的一些相关:
基于 NodeList Class 是个密封类. 我们可以初步了解StaticNodeList 并不像最初我认为的那样,可能派生自NodeList. 而且规范说的明白. 这个集合是静态的.就是说它不会随着DOM树的变化而变化. 这种选择性去除基类能力的做法不符合继承的思想.所以只可能是另外的一个东东了.

Members Table

The following table lists the members exposed by the StaticNodeList object.

Attributes/Properties
Property Description
length Gets the number of characters in a TextNode object.
Methods

上一个:基于jquery的关于动态创建DOM元素的问题
下一个:Javascript动态绑定事件的简单实现代码

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,
Method