首页 > 语言 > JavaScript > 正文

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

2024-05-06 14:25:56
字体:
来源:转载
供稿:网友
在您开始本文的阅读前,我强烈建议您可以先读一读此篇:http://w3help.org/zh-cn/causes/SD9004.
          HTMLCollection 接口定义

 

interface 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 接口定义
interface 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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表