使用JavaScript原生态的语法删除html文档的节点,具体操作方法见下面两个案例:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script language="javascript"> function closeRecommend(obj){ obj.parentNode.parentNode.removeChild(obj.parentNode); } function clearText() { var content=document.getElementById("content"); var len=content.childNodes.length; for(var i=0;i<len;i++){ var childNode = content.childNodes[0]; content.removeChild(childNode); } } </script> </head> <body> <div id="content"> <h1>1</h1> <h1>2</h1> <h1>3</h1> </div> <button onclick="clearText();">清除节点内容</button> <div id="content"> <h1>1</h1> <h1>2</h1> <h1>3</h1> <button onclick="closeRecommend(this);">清除节点内容</button> </div> </body> </html>