大家好,我想问一下,如何能将当前的页的html标签过滤掉,但还要求保留input的value值,例如说:
问题:<textarea name="textfield23" cols="80" rows="8">测试</textarea>
最后就变成->问题:测试

解决方案 »

  1.   

    去掉所有的<,>以及之间的东西就行了吧,但前提是页面代码必须规范
      

  2.   

    通过正则表达式,把<和>之间的东西替换为空字符串就行了。
      

  3.   

    public static String html2Text(String inputString) {
          String htmlStr = inputString; //含html标签的字符串
          String textStr ="";
          java.util.regex.Pattern p_script;
          java.util.regex.Matcher m_script;
          java.util.regex.Pattern p_style;
          java.util.regex.Matcher m_style;
          java.util.regex.Pattern p_html;
          java.util.regex.Matcher m_html;       try {
           String regEx_script ="<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\s\S]*?<\/script> }
           String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\s\S]*?<\/style> }
              String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式           p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
              m_script = p_script.matcher(htmlStr);
              htmlStr = m_script.replaceAll(""); //过滤script标签           p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
              m_style = p_style.matcher(htmlStr);
              htmlStr = m_style.replaceAll(""); //过滤style标签           p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
              m_html = p_html.matcher(htmlStr);
              htmlStr = m_html.replaceAll(""); //过滤html标签        textStr = htmlStr;
          }catch(Exception e) {
                      System.err.println("Html2Text: " + e.getMessage());
          }       return textStr;//返回文本字符串
        }
      

  4.   

    你这个会把我的input 中的value的值滤掉的,所以不行。嘿嘿,有没有高招啊,大家看来都不用这个啊,唉。我这个问题怎么这么怪呢!
      

  5.   

    不行的,我想到了一个方法,但也只是要在一定的条件下才能行,我放出来,大家看看,希望大家给出新的想法.
    function showText(sqart)
    {

     var text = "";
     
     for(var i=0;i<document.body.childNodes.length;i++)
     {
      var n = document.body.childNodes[i];
      if(n.tagName == 'SPAN'){
      text += n.innerHTML;
      }
      if(n.tagName == 'INPUT'&&n.type == 'text'){
      text += n.value + sqart;
      }
      if(n.tagName == 'TEXTAREA'){
      text += n.value + sqart;
      }
     
     }
     
    document.getElementById('text').value = text;
    document.form1.submit();
    }
    我现在写的这个东西只能过滤几种标签,希望大家能给出更好的解决方案