---
title: "Js获取所有文本节点"
author: "adminsun"
date: "2009-11-30T00:25:00Z"
tags: []
slug: "js"
draft: false
lastmod: "2009-11-30T00:25:16Z"
id: 534295
url: "https://adminsun.iteye.com/blog/534295"
---

```
<script type="text/javascript">
var contents = "";
function extractDocument(el) {
    
    var childNodes = el.childNodes;
    for (var i = 0; i < childNodes.length; i ++) {
        var c = childNodes[i];
        switch(c.nodeType) {
            case 1:
			   if(c.nodeName=="A")c;
                extractDocument(c);
                break;
            case 3:
                if (trim(c.nodeValue).length == 0) break;
                contents += " | " + c.nodeValue;
                break;
        }
    }
}
function trim(str) {
     return str.replace(/(^\s*)|(\s*$)/g, "");  
}
</script>
onload="extractDocument(document.body);alert(contents);
```