一组“dl”列表:
<div class = "container">
<dl class = "list">
<dt class = "title">title 1-1</dt>
<dd class = "detail">detail 1-1</dd>
<dt class = "title">title 1-2</dt>
<dd class = "detail">detail 1-2</dd>
</dl>
</div>
<div class = "container">
<dl class = "list">
<dt class = "title">title 2-1</dt>
<dd class = "detail">detail 2-1</dd>
<dt class = "title">title 2-2</dt>
<dd class = "detail">detail 2-2</dd>
</dl>
</div>
我需要访问“dt”标签中的所有标题。节点列表返回了两个节点,但是一旦我将它转换为一个数组,它在 console.log 中什么也没有显示。
var myContainer = document.getElementsByClassName('container');
for(var i=0; i<myContainer.length; i++) {
var nodelist = myContainer[i].getElementsByTagName('dt');
console.log(nodelist); //returns HTMLcollection []
// 0 - dt.title
// 1 - dt.title
var arr = Array.prototype.slice.call(nodelist, 0);
console.log(arr); //returns []
//there are no child objects.
}
这是为什么?