创建一个 HTMLCollection

发布于 2024-12-09 18:59:08 字数 783 浏览 0 评论 0原文

我正在尝试填充 Element.prototype.children 其中应该返回一个 HTMLCollection

有一个 window.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唱一曲作罢 2024-12-16 18:59:08

我认为这是创建 HTMLCollection 的正确方法,由浏览器处理。

var docFragment = document.createDocumentFragment();
docFragment.appendChild(node1);
docFragment.appendChild(node2);
var myHTMLCollection = docFragment.children;

参考文献:

https://stackoverflow.com/a/35969890/10018427

https://developer.mozilla.org/en-US/文档/Web/API/NodeList

https://developer.mozilla.org/en-US/文档/Web/API/HTMLCollection

https://www.w3schools.com/js/js_htmldom_nodelist.asp< /p>

I think this is the proper way of creating HTMLCollection, which is handled by the browser.

var docFragment = document.createDocumentFragment();
docFragment.appendChild(node1);
docFragment.appendChild(node2);
var myHTMLCollection = docFragment.children;

Refs.:

https://stackoverflow.com/a/35969890/10018427

https://developer.mozilla.org/en-US/docs/Web/API/NodeList

https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection

https://www.w3schools.com/js/js_htmldom_nodelist.asp

遇见了你 2024-12-16 18:59:08

我的做法如下:

function MyHTMLCollection( arr ) {
    for ( var i = 0; i < arr.length; i += 1 ) {
        this[i] = arr[i];
    }

    // length is readonly
    Object.defineProperty( this, 'length', {
        get: function () {
            return arr.length;
        }
    });

    // a HTMLCollection is immutable
    Object.freeze( this );
}

MyHTMLCollection.prototype = {
    item: function ( i ) {
        return this[i] != null ? this[i] : null;
    },
    namedItem: function ( name ) {
        for ( var i = 0; i < this.length; i += 1 ) {
            if ( this[i].id === name || this[i].name === name ) {
                return this[i];
            }
        }
        return null;
    }
};

其中 arr 是一个常规数组,其中包含应位于 HTMLCollection 内的所有 DOM 元素。

待办事项列表:

  • 应事先检查参数 arr:它是数组吗?该数组的所有元素都是 DOM 元素吗?

Here's how I would do it:

function MyHTMLCollection( arr ) {
    for ( var i = 0; i < arr.length; i += 1 ) {
        this[i] = arr[i];
    }

    // length is readonly
    Object.defineProperty( this, 'length', {
        get: function () {
            return arr.length;
        }
    });

    // a HTMLCollection is immutable
    Object.freeze( this );
}

MyHTMLCollection.prototype = {
    item: function ( i ) {
        return this[i] != null ? this[i] : null;
    },
    namedItem: function ( name ) {
        for ( var i = 0; i < this.length; i += 1 ) {
            if ( this[i].id === name || this[i].name === name ) {
                return this[i];
            }
        }
        return null;
    }
};

where arr is a regular array that contains all the DOM elements which should be inside the HTMLCollection.

To do list:

  • the argument arr should be checked beforehand: Is it an array? Are all elements of that array DOM elements?
无法言说的痛 2024-12-16 18:59:08

不要期望宿主对象的行为类似于(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.

好倦 2024-12-16 18:59:08

我知道这是一个较旧的问题,但我遇到了类似的创建空 HTMLCollection 的需求,我通过简单地创建一个元素,然后使用元素中不存在的类对其运行 getElementsByClassName() 来做到这一点。

document.createElement("div").getElementsByClassName('noClassHere');

这将返回一个空的 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.

document.createElement("div").getElementsByClassName('noClassHere');

This returns an empty HTMLCollection object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文