为什么当我记录tagName
特定节点列表中的所有节点时,它会给我这个重复的undefined
反馈?
这是我从中提取节点的代码的一部分:
索引.html
<form name="contactForm" id="contactForm">
<div id="textInfo">
<ul>
<li>
<label for="firstName" class="mainLabel">First Name : </label>
<input type="text" name="firstName" id="firstName"/>
<span>This must be filled</span>
</li>
<li>
<label for="lastName" class="mainLabel">Last Name : </label>
<input type="text" name="lastName" id="lastName"></input>
<span>This must be filled</span>
</li>
<li>
<label for="email" class="mainLabel">E-mail : </label>
<input type="email" name="email" id="email"></input>
<span>This must be filled</span>
</li>
</ul>
</div>
脚本.js
var myForm = document.forms["contactForm"];
eventUtil.add(myForm, "submit" , function(evt){
var firstName = myForm.elements["firstName"];
if(firstName.value == ""){
for(i=0; i < firstName.parentNode.childNodes.length ; i++){
console.log("childNodes[" + i + "]: " + firstName.parentNode.childNodes[i].tagName);
}
eventUtil.preventDefault(evt);
}
});
输出是:
childNodes[0]: undefined
childNodes[1]: LABEL
childNodes[2]: undefined
childNodes[3]: INPUT
childNodes[4]: undefined
childNodes[5]: SPAN
childNodes[6]: undefined
为什么它undefined
反复给我那个输出?