【发布时间】:2012-07-28 07:13:38
【问题描述】:
查看代码:
<!DOCTYPE html>
<meta charset="utf-8">
<title>An HTML5 Document</title>
<p>
<p>
<script>
var a = [1, 2],
b = [3, 4],
c = a.concat(b),
d, e, f, g;
console.log(c); // No problem
d = [document.querySelectorAll('p')[0], document.querySelectorAll('p')[1]];
e = a.concat(d);
console.log(e); // No problem
f = document.querySelectorAll('p'); // f = document.getElementsByTagName('p');
g = a.concat(f);
console.log(g); // Pretty strange...
</script>
jsFiddle:http://jsfiddle.net/V7gmE
我的问题是:
c.length 是 4。没有问题。
e.length 是 4。没问题。
如果我使用f = document.querySelectorAll('p'); 或f = document.getElementsByTagName('p');,为什么g.length 是'3'?
谢谢。
【问题讨论】:
-
这不是真正的生产代码,对吧?
d = [document.querySelectorAll('p')[0], document.querySelectorAll('p')[1]];无缘无故将您的处理量翻倍。 -
这些代码是专门为这个问题写的。
标签: getelementsbytagname dom selectors-api