本文翻译自:Most efficient way to convert an HTMLCollection to an Array
有没有更有效的方法将HTMLCollection转换为数组,除了迭代所述集合的内容并手动将每个项目推入数组?
#1楼
参考:https://stackoom.com/question/vyD/将HTMLCollection转换为数组的最有效方法
#2楼
This is my personal solution, based on the information here (this thread): 这是我的个人解决方案,基于此处的信息(此主题):
var Divs = new Array();
var Elemns = document.getElementsByClassName("divisao");
try {
Divs = Elemns.prototype.slice.call(Elemns);
} catch(e) {
Divs = $A(Elemns);
}
Where $A was described by Gareth Davis in his post: Gareth Davis在他的帖子中描述了A的情况:
function $A(iterable) {
if (!iterable) return [];
if ('toArray' in Object(iterable)) return iterable.toArray();
var length = iterable.length || 0, results = new Array(length);
while (length