【发布时间】:2011-10-13 12:40:31
【问题描述】:
我正在尝试填充 Element.prototype.children,它应该返回一个 HTMLCollection
然而
var h = new HTMLCollection();
//TypeErrror: HTMLCollection is not a constructor
和
var h = Object.create(HTMLCollection.prototype);
h[0] = div;
h.item(0);
// Could not convert JavaScript argument
测试 Firefox 7 和 Chrome
除了 shiming HTMLCollection 有什么方法可以与之交互吗?
如果您可以提出解决方案,请在this github issue 上提供反馈
【问题讨论】:
-
我认为正确的做法是定义自己的自定义
MyHTMLCollection构造函数,然后使用它代替宿主构造函数HTMLCollection(这不可靠) -
虽然我无法回答您关于 HTMLCollection 的具体问题,但扩展本机 DOM(托管)对象通常被认为是不好的做法。详细解释见这篇文章:perfectionkills.com/whats-wrong-with-extending-the-dom
-
@skyline3000 我完全清楚后果。我们需要这样做来生成一个 DOM-shim。我们不是使用自定义方法(邪恶)扩展 DOM,而是使用根据 DOM4 规范应该存在的方法
标签: javascript dom dom4