求问大神们我的代码错在哪里···

来源:9-14 删除节点removeChild()

File_inthehole

2015-09-01 19:15

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

</head>


<body>

<div id="content">

  <h1>html</h1>

  <h1>php</h1>

  <h1>javascript</h1>

  <h1>jquery</h1>

  <h1>java</h1>

</div>


<script type="text/javascript">

function clearText() {

  var content=document.getElementById("content");

  // 在此完成该函数

  while((content.childNodes.length>0) && (content.childNodes[0].nodeType!=1))

     { 

         content.removeChild(content.childNodes[0]);

     }


}

document.write(content.childNodes[0].nodeType!=1);

</script>


<button onclick="clearText()">清除节点内容</button>




</body>

</html>


写回答 关注

1回答

  • haodaking
    2015-09-01 22:26:06
    已采纳

    上段代码只执行了一次,删除了文本节点,下次执行的时候nodeType=1了;while里的执行不了,条件&&改成||,

    File_i...

    非常感谢!

    2015-09-02 10:37:16

    共 1 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

467393 学习 · 21877 问题

查看课程

相似问题