从页面获取所有文本作为字符串

Get all text as a string from page

我想将 html 页面中的所有文本转换为字符串,然后在控制台上打印出来。到目前为止,我的这段代码只能部分工作,因为 document.body.innerText returns 文本和我当然不想要的线制动器。我希望我所有的文字都显示在 1 行中

<html>
    <head>
        <h1>wtf</h1>
    </head>
    <body>
        <div>dddiiiv</div>
        <a>aaaaa</a>
        <p>ppp</p>
        <div>div</div>
        <div id="impar"></div>
        <div id="par"></div>
        <div id="all"></div>
        <script>
            var elems = document.body.innerText;
            var odd = "";
            var even = "";
            var all = "";
            for (k in elems){
                all = all + elems[k];
                if (k % 2 === 0) {
                    even = even + elems[k]
                } else {
                    odd = odd + elems[k]
                }
            }


            console.log(all);
            console.log(odd);
            console.log(even);
        </script>
    </body>
</html>

有没有绕过 innerText 刹车的想法?

我用过 .trim() 和 codepen URL 作为参考 -http://codepen.io/nagasai/pen/jrPpeK

    <html>
        <head>
            <h1>wtf</h1>
        </head>
        <body>
            <div>dddiiiv</div>
            <a>aaaaa</a>
            <p>ppp</p>
            <div>div</div>
            <div id="impar"></div>
            <div id="par"></div>
            <div id="all"></div>

        </body>
    </html>

Javascript

    var elems = document.body.innerText;
                var odd = "";
                var even = "";
                var all = "";
                for (k in elems){
                    all = all + elems[k];
                    if (k % 2 === 0) {
                        even = (even + elems[k]).trim();
                    } else {
                        odd = (odd + elems[k]).trim();
                    }
                }


                console.log(all);
                console.log(odd);
                console.log(even);

希望对您有所帮助