我试图克隆一个节点,它是另一个节点的子节点,但是 js 不允许我索引 HTMLCollection/NodeList 数据类型。每当我尝试索引该数据类型时,它总是会导致未定义的值。即使当我按照将 HTMLCollection 转换为数组的最有效方式将该数据类型转换为数组时,我仍然无法索引该数组。
var viewports = document.getElementsByClassName('imageViewerViewport');
console.log(viewports[0].childNodes) // In the console shows the children I expect to see
console.log(viewports[0].childNodes.item(0)) // Nothing shows up - undefined
console.log(viewports[0].childNodes.item(1)) // Nothing shows up - undefined
console.log(viewports[0].children.item(0)) // Nothing shows up - undefined
console.log(viewports[0].children.item(1)) // Nothing shows up - undefined
console.log(viewports[0].childNodes[0]) // Nothing shows up - undefined
console.log(viewports[0].childNodes[1]) // Nothing shows up - undefined
console.log(viewports[0].children[0]) // Nothing shows up - undefined
console.log(viewports[0].children[1]) // Nothing shows up - undefined
console.log("Here")
var second_canvas = viewports[0].children.item(1).cloneNode(true); // Error that is detailed below
viewports[1].appendChild(second_canvas);
克隆节点给出的错误:Cannot read property 'cloneNode' of undefined
。