为什么我的DOCUMENT不会输出内容呢
var otest = document.getElementById("test");
var newotest=document.createElement("li");
newotest.innerHTML="PHP";
otest.appendChild(newotest);
for(var i=0;i<otest.length;i++){
document.write(otest[i].nodeName);
}
var otest = document.getElementById("test");
var newotest=document.createElement("li");
newotest.innerHTML="PHP";
otest.appendChild(newotest);
for(var i=0;i<otest.length;i++){
document.write(otest[i].nodeName);
}
2019-07-04
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<ul id="test">
<li>JavaScript</li>
<li>HTML</li>
</ul>
<script type="text/javascript">
var otest = document.getElementById("test");
var newotest = document.createElement("li");
newotest.innerHTML = "PHP";
otest.appendChild(newotest);
for(var i = 0; i < otest.childNodes.length; i++) {
console.log(otest.childNodes.length);
document.write(otest.childNodes[i].nodeName);
}
</script>
</body>
</html>
你代码应该写成这样,因为你otest是getElementById得到的,是ul这一个节点,而不是其下面的li子节点。
举报