删除HTML对象所有子节点

Posted on 2006-1-25 星期三

此函数可以删除指定HTML对象的所有自节点。

  1. function removeChildrenFromNode(node) {
  2.         if(node == undefined || node == null) {
  3.                return;
  4.         }
  5.         var len = node.childNodes.length;
  6.         while (node.hasChildNodes()) {
  7.                 node.removeChild(node.firstChild);
  8.         }
  9. }

实例代码:

  1. <!doctype html public “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
  2. <html xmlns=“http://www.w3.org/1999/xhtml”>
  3. <title></title>
  4. <script language=“javascript” type=“text/javascript”>
  5. //<![cdata[
  6.  
  7. function rm(){
  8. obj = document.getElementById(“tb”);
  9. removeChildrenFromNode(obj)
  10. }
  11. function removeChildrenFromNode(node)
  12. {
  13.    if(node == undefined || node == null)
  14.    {
  15.       return;
  16.    }
  17.    //alert(node);
  18.    var len = node.childNodes.length;
  19.    
  20.         while (node.hasChildNodes())
  21.         {
  22.           node.removeChild(node.firstChild);
  23.         }
  24. }
  25.  
  26. //]]>
  27. </script>
  28. </head>
  29.  
  30. <table width=“100%” cellspacing=“0″ id=“tb”>
  31. <tr><td>1</td></tr>
  32. <tr><td>2</td></tr>
  33. <tr><td>3</td></tr>
  34. <tr><td>4</td></tr>
  35. <tr><td>5</td></tr>
  36. <tr><td>6</td></tr>
  37. <tr><td>7</td></tr>
  38. </table>
  39.  
  40. <input type=“reset” value=“tt” onclick=“rm();”>
  41. </center>
  42. </body></html>

No comments have been added to this post yet.

留下评论

(必需)

(必需)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI