Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从HTMLCollection输出所有项_Javascript_Html - Fatal编程技术网

Javascript 从HTMLCollection输出所有项

Javascript 从HTMLCollection输出所有项,javascript,html,Javascript,Html,我有一个从字符串转换而来的HTMLCollection: let el = document.createElement('html'); el.innerHTML = data; // This data is my HTML string let allArtistTiles = el.getElementsByClassName('artistTile'); console.log(allArtistTiles); // I used this to check I see the

我有一个从字符串转换而来的
HTMLCollection

let el = document.createElement('html');

el.innerHTML = data; // This data is my HTML string

let allArtistTiles = el.getElementsByClassName('artistTile');

console.log(allArtistTiles); // I used this to check I see the HTML Collection

let artistsPlaceholder = document.getElementById('artistsPlaceholder')

artistsPlaceholder.innerHTML = allArtistTiles

这是我不确定的最后一行。如何实际显示
HTMLCollection
中的所有元素?我必须遍历HTMLCollection吗?另外,是否需要将HTMLCollection转换为数组?

artistsPlaceholder.innerHTML=allArtistTiles
无法工作,因为
innerHTML
setter需要一个字符串。您需要附加这些项。在现代浏览器中,您可以使用

artistsPlaceholder.append(...allArtistTiles);
这需要对和的支持


或者,你可以写

Array.from(allArtistTiles).forEach((elem) => artistsPlaceholder.appendChild(elem));
它使用(可由
数组.prototype.slice.call替换)和(可由常规函数替换)


如果要对
循环使用
,仍然建议在数组上循环(例如从
array.prototype.slice.call(allArtistTiles)
)以避免


严格来说,没有必要将
HTMLCollection
转换为
数组
,但是
数组
更容易使用。

artistsPlaceholder.innerHTML=allArtistTiles
无法工作,因为
innerHTML
setter需要一个字符串。您需要附加这些项。在现代浏览器中,您可以使用

artistsPlaceholder.append(...allArtistTiles);
这需要对和的支持


或者,你可以写

Array.from(allArtistTiles).forEach((elem) => artistsPlaceholder.appendChild(elem));
它使用(可由
数组.prototype.slice.call替换)和(可由常规函数替换)


如果要对
循环使用
,仍然建议在数组上循环(例如从
array.prototype.slice.call(allArtistTiles)
)以避免


严格来说,没有必要将
HTMLCollection
转换为
数组
,但是
数组
更容易使用。

innerHTML
需要的是字符串,而不是对象。使用artistsPlaceholder.append(…allArtistTiles)如果可以使用and。
innerHTML
需要字符串,而不是对象。使用artistsPlaceholder.append(…allArtistTiles)如果可以使用和。