回覆列表
  • 1 # lylit17535

    Html中特殊字元不被轉義,可以使用預格式化標籤。  pre 是 Preformatted text(預格式化文字) 的縮寫。使用此標籤可以把程式碼中的空格和換行直接顯示到頁面上。  例如HTML程式碼: 12345

     if (xx > 5) { print "比5大!\n"; } 

      瀏覽器顯示效果:  if (xx > 5) {  print "比5大!\n";  }  之間包含有類似的這種跳脫字元的時候總會被解析,倒是可以把所有的"&"透過程式替換成"&",但是有些本來就是"&"的也會被轉換,這就錯了。如何讓之間包含的文字原封不動的顯示出來呢?  總結如下:  解決方法有兩種:  第1種:  123456        第2種:  /*將字串轉為html格式*/ 1234567891011121314151617181920212223public String strToHtml(String s) { if (s==null||s.equals("")) return ""; s = s.replaceAll("&", "&"); s = s.replaceAll("", ">"); s = s.replaceAll(" ", " "); // s = s.replaceAll("/n", ""); // s = s.replaceAll(""", """); return s; } /*將html格式轉為字串*/ public String strToHtml(String s) { if (s==null||s.equals("")) return ""; s = s.replaceAll("&","&"); s = s.replaceAll("",">"); s = s.replaceAll(" "," "); //s = s.replaceAll("","/n"); //s = s.replaceAll(""","""); return s; }  最後一點:jQuery的.html()方法預設會轉義的,這種情況使用.text()就不會轉義了。

  • 中秋節和大豐收的關聯?
  • 寫木棉樹的好句?