你的另一个帖子地址,帖出来方便以后全文检索功能查询
http://community.csdn.net/Expert/topic/4826/4826035.xml?temp=.9429132
是可以用document.getElementById()的,不过要注意执行顺序,只要把textarea放在最上面先执行就可以了。
因为字符串中有换行,所以不能直接写在JS里。

解决方案 »

  1.   

    但是我现在的需求是这样的:
    在脚本中获得把从服务器(jsp)中传来的有换行符的字符串,判断里面有没有某一个子串,如果有,才在页面显示的一个控件里另外一个数据,所以我必须在页面生成之前执行脚本,而不是先显示在一个textarea里,再用document.getElementById()取出来。javascript有解决的办法嘛?
      

  2.   

    比如"\n",在服务器端jsp中写成"\\n",因为要有2次转义
      

  3.   

    服务端数据输出到html里经经过转码:
    <script >
    var a="<%=CommStr.jsEncoder(serverStr)%>";
    alert(a);
    </script>
    <input name=a value="<%=CommStr.htmlEncoder(serverStr)%>">
    ---------------
    /*
     * Created by Seamus
     * Updated on 2004-10-21 by JK
     *
     */
    package com.jk.util;public class  CommStr
    {
      /**
      *replace the old string to new string in the given destination string.
      */
      public static java.lang.String strReplace(java.lang.String destStr, java.lang.String oldStr, java.lang.String newStr)
      {
        if(destStr==null)
          return "";
        String tmpStr = destStr;
        int foundPos = tmpStr.indexOf(oldStr);
        while (foundPos>=0)
        {
          tmpStr = tmpStr.substring(0,foundPos) + newStr + tmpStr.substring(foundPos + oldStr.length(),tmpStr.length());
          foundPos = tmpStr.indexOf(oldStr,foundPos+newStr.length());
        }
        return tmpStr;
      }  /**
      *Encode for HTML.
      */
      public static String htmlEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"&","&amp;");
        res_str=strReplace(str," ","&nbsp;");
        res_str=strReplace(str,"<","&lt;");
        res_str=strReplace(str,">","&rt;");
        res_str=strReplace(str,"\"","&quot;");
        res_str=strReplace(str,"'","&#039;");
        return res_str;
      }  /**
      *Encode for HTML-Text.
      */
      public static String htmlTextEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"&","&amp;");
        res_str=strReplace(str,"<","&lt;");
        res_str=strReplace(str,">","&rt;");
        res_str=strReplace(str,"\"","&quot;");
        res_str=strReplace(str,"'","&#039;");
        res_str=strReplace(str," ","&nbsp;");
        res_str=strReplace(str,"\r\n","<br>");
        res_str=strReplace(str,"\r","<br>");
        res_str=strReplace(str,"\n","<br>");
        return res_str;
      }  /**
      *Encode for URL.
      */
      public static String urlEncoder(String str) {
        return java.net.URLEncoder.encode(str) ;
      }  /**
      *Encode for XML.
      */
      public static String xmlEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"&","&amp;");
        res_str=strReplace(res_str,"<","&lt;");
        res_str=strReplace(res_str,">","&gt;");
        res_str=strReplace(res_str,"\"", "&quot;");
        res_str=strReplace(res_str,"\'", "&acute;");
        return res_str;
      }  /**
      *Encode for SQL.
      */
      public static String sqlEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"'","''");
        return res_str;
      }  /**
      *Encode for Javascript.
      */
      public static String jsEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"'","\\'");
        res_str=strReplace(str,"\"","\\\"");
        res_str=strReplace(str,"\r\n","\\n");
        res_str=strReplace(str,"\n","\\n");
        res_str=strReplace(str,"\r","\\n");
        return res_str;
      }}