慕尼黑3533373
2020-02-18 14:53
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; 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 arr = otest.childNodes;
for (var i = 0; i < arr.length; i++){
if (arr[i].innerHTML == "HTML"){
var newnode = document.createElement("li");
newnode.innerHTML = "php";
otest.insertBefore(newnode,arr[i]);
}
}
</script>
</body>
</html>
用lastChild更简洁吧
遍历循环啊,你不给结束,它就一直循环下去,所以卡死啊。
遍历循环啊,你不终止它,它就一直循环下去啊,会卡死,加 break啊;
for (var i = 0; i < arr.length; i++){
if (arr[i].innerHTML == "HTML"){
var newnode = document.createElement("li");
newnode.innerHTML = "php";
otest.insertBefore(newnode,arr[i]);
break;
}
}
2-18的?你现在应该知道了吧,那我就不回答了
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题