创建一个 HTMLCollection
我正在尝试填充 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
除了填充 HTMLCollection 之外 code> 有什么办法可以与之交互吗?
如果您可以提出解决方案,还可以提供有关此 github 问题的反馈
I'm trying to shim Element.prototype.children
which should return a HTMLCollection
There is a window.HTMLCollection
However
var h = new HTMLCollection();
//TypeErrror: HTMLCollection is not a constructor
and
var h = Object.create(HTMLCollection.prototype);
h[0] = div;
h.item(0);
// Could not convert JavaScript argument
Test Firefox 7 and Chrome
Apart from shimming HTMLCollection
is there any way to interact with it?
Also provide feedback on this github issue if you can suggest a solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这是创建 HTMLCollection 的正确方法,由浏览器处理。
参考文献:
I think this is the proper way of creating HTMLCollection, which is handled by the browser.
Refs.:
我的做法如下:
其中
arr
是一个常规数组,其中包含应位于 HTMLCollection 内的所有 DOM 元素。待办事项列表:
arr
:它是数组吗?该数组的所有元素都是 DOM 元素吗?Here's how I would do it:
where
arr
is a regular array that contains all the DOM elements which should be inside the HTMLCollection.To do list:
arr
should be checked beforehand: Is it an array? Are all elements of that array DOM elements?不要期望宿主对象的行为类似于(ECMAScript)本机对象,它们是完全不同的东西。有些浏览器确实像 ECMAScript 对象一样实现了 DOM 对象,但这不是必需的,也不应该依赖。请注意,大多数 HTML 集合都是实时的,很难在本机对象中模拟它。
Don't expect host objects to behave like (ECMAScript) native objects, they are completely different things. Some browsers do implement their DOM objects like ECMAScript objects, but it is not required and should not be relied upon. Note that most HTML collections are live, it is very difficult to emulate that in a native object.
我知道这是一个较旧的问题,但我遇到了类似的创建空 HTMLCollection 的需求,我通过简单地创建一个元素,然后使用元素中不存在的类对其运行 getElementsByClassName() 来做到这一点。
这将返回一个空的 HTMLCollection 对象。
I know this is an older question but I came across a similar need to create an empty HTMLCollection and I did it by simply creating an element and then running getElementsByClassName() against it using a class that doesn't exist in the element.
This returns an empty HTMLCollection object.