<!DOCTYPE html>
<meta charset="utf-8">
<title>An HTML5 Document</title>
<p>1
<p>2
<p>3
<p>4
<p>5
<p>6
<p>7
<p>8
<p>9
<p>10
<p>11
<p>12
<script>
// When the document is clicked, codes below should remove all the paragraphs in the page.
// There are 12 paragraphs in the page, but codes below can just remove 6 of them.
document.addEventListener('click', function () {
var i, allParagraphElements = document.getElementsByTagName('p');
console.log('allParagraphElements.length: ' + allParagraphElements.length);
for (i = 0; i < allParagraphElements.length; i += 1) {
console.log('i: ' + i);
allParagraphElements[i].parentNode.removeChild(allParagraphElements[i]);
}
}, false);
</script>
查看的jsfiddle代码: http://jsfiddle.net/7NmRh
我怎样才能解决这个问题呢? 谢谢。