function getContentInBody(stype){
    var html=document.body.innerHTML;
    switch(stype){
        case "html":
           return html;
        break;
           return html.replace(/<.*?>/gi,"");
        case "text":
        break
    }
}

解决方案 »

  1.   


    <script>
    var a='<body> 文本内容 </body> <body> <BODY> <font   style="color:red"> bbb </font> </BODY> </body>'
    alert(a.replace(/^<body>(.*?)<\/body>.*$/i,"$1"))
    alert(a.replace(/^.*?<body> *<BODY>(.*?)<\/BODY> *<\/body>$/i,"$1"))
    </script>
      

  2.   

    好像楼主的代码离起码的规范还差10万8k miles,一个html document怎么有2个body呢?
      

  3.   

    # hanpoyangtitan
    这不是html document,是可扩展出席信息协议XMPP的格式,不过这里我暂时只当它字符串处理了,其实是个
    XMLDocument
      

  4.   

    # mingxuan3000 朋友的代码近乎满足要求了,但在实际中比例子复杂些,当<>标签里有内容上时就不行了
    下面是实际接收到的一段XMLDoc,提供了读的2个接口,可以接受为XMLDocument<message from="[email protected]/resource1" id="rm324_69" to="[email protected]/#2285AT2008-1-28 10:12:14" type="chat"><body>文本消息</body><html xmlns="http://jabber.org/protocol/xhtml-im"><body xmlns="http://www.w3.org/1999/xhtml"><BODY><FONT color="#0000ff" face="&quot;华文行楷&quot;" size="5">文本消息</FONT></BODY></body></html><x xmlns="jabber:x:event"><composing/></x></message>也可以接受为字符串   var strMessage = '<message from="[email protected]/resource1" id="rm324_69" to="[email protected]/#2285AT2008-1-28 10:12:14" type="chat"><body>文本消息</body><html xmlns="http://jabber.org/protocol/xhtml-im"><body xmlns="http://www.w3.org/1999/xhtml"><BODY><FONT color="#0000ff" face="&quot;华文行楷&quot;" size="5">文本消息</FONT></BODY></body></html><x xmlns="jabber:x:event"><composing/></x></message>'只要能顺利拿到普通文本的信息和带格式的文本信息就可以了,加点分吧,提高大家的奋斗精神
      

  5.   

    小做改动即可:
    <script>
    var a='<body xmlns="http://www.w3.org/1999/xhtml"> 文本内容 </body> <body> <BODY> <font   style="color:red"> bbb </font> </BODY> </body>'
    alert(a.replace(/^<body.*?>(.*?)<\/body>.*$/i,"$1"))
    alert(a.replace(/^.*?<body.*?> *<BODY.*?>(.*?)<\/BODY> *<\/body>$/i,"$1"))
    </script>
      

  6.   


    <script>
    var a='<body aa> 文本内容 </body> <body style="123"> <BODY style="123"> <font   style="color:red"> bbb </font> </BODY> </body>'
    alert(a.replace(/^<body[^>]*?>(.*?)<\/body[^>]*?>.*$/i,"$1"))
    alert(a.replace(/^.*?<body[^>]*?> *<BODY[^>]*?>(.*?)<\/BODY[^>]*?> *<\/body[^>]*?>$/i,"$1"))
    </script>
      

  7.   

    哎,再怎么复杂也万变不离其中!只是你没有换度进步思考!一味等别人给你全面的答案。
    mingxuan3000 之前给你的,稍微改动下就完全可以!
      

  8.   


    # hanpoyangtitan
    =======================
    我竟然发现,即使在HTML里的,竟然是可以有2个body的,偶然发现的,但是符合不符合标准就不清楚。
    有没有朋友提供一些w3c Doc的解决方法呢?
      

  9.   

    取巧,只取><之间的
    导入prototype1.6.js
    <script src="prototype1.6.js"></script>
    ...var   strMessage   =   ' <message   from="[email protected]/resource1"   id="rm324_69"   to="[email protected]/#2285AT2008-1-28   10:12:14"   type="chat"> <body> 文本消息 </body> <html   xmlns="http://jabber.org/protocol/xhtml-im"> <body   xmlns="http://www.w3.org/1999/xhtml"> <BODY> <FONT   color="#0000ff"   face="&quot;华文行楷&quot;"   size="5"> 文本消息2 </FONT> </BODY> </body> </html> <x   xmlns="jabber:x:event"> <composing/> </x> </message> ' //arr是一个数组,用于装结果
    var arr = [];
    strMessage.scan(/(>)(.*?)(<)/, function(match){ if(!match[2].blank()) arr.push(match[2])});
    alert(arr.inspect())
    结果:['文本消息 ','文本消息2']