大家看看这段怎么样,有什么隐患。

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

无知的狗3576298

2016-08-08 17:29

<!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");

  // 在此完成该函数

  var num = content.childNodes.length;

  function reduce(){

  if(content.childNodes.length>0){

      content.removeChild(content.firstChild);

      reduce();

    }

  }

  reduce()

}

</script>

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

</body>

</html>

写回答 关注

1回答

  • RDS_ray
    2016-08-09 10:00:13

    js脚本最好放在head里

JavaScript进阶篇

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

467356 学习 · 21877 问题

查看课程

相似问题