4,JavaScript中escape/unescapeHTML的escape,encodeURI/decodeURI做URL的encode/decode
Java代码 复制代码
  1. var a = "<span>afd adf &&& <<< >>></span>"  
  2. var b = escape(a)   
  3.   => "%3Cspan%3Eafd%20adf%20%26%26%26%20%3C%3C%3C%20%3E%3E%3E%3C/span%3E"  
  4. var c = unescape(b)   
  5.   => "<span>afd adf &&& <<< >>></span>"  
  6.   
  7. var a = "http://www.test.com/haha hehe/"  
  8. var b = encodeURI(a)   
  9.   => "http://www.test.com/haha%20hehe/"  
  10. var c = decodeURI(b)   
  11.   => "http://www.test.com/haha hehe/"  


5,jQuery中的text(str)和html(str)
jQuery中text(str)方法给某个HTML Element设置文本内容,其中的文本如果包括HTML Tag或者<script>标签都会忽略而当作文本来看待
jQuery中html(str)方法给某个HTML Element设置html内容,其中的内容如果包括HTML Tag或者<script>标签则都会当作正常标签来执行

6,JavaScript实现h和uh方法来escape/unescape HTML
该方法用于JavaScript拼接HTML片段时防止&、<、>、"相关的问题
Java代码 复制代码
  1. function escapeHTML(str) {   
  2.   str = String(str).replace(/&/g, '&amp;').   
  3.     replace(/>/g, '&gt;').   
  4.     replace(/</g, '&lt;').   
  5.     replace(/"/g, '&quot;');   
  6.   return str;   
  7. }   
  8. function unescapeHTML(str) {   
  9.   str = String(str).replace(/&gt;/g, '>').   
  10.     replace(/&lt;/g, '<').   
  11.     replace(/&quot;/g, '"').   
  12.     replace(/&amp;/g, '&');   
  13.   return str;   
  14. }   
  15. h = escapeHTML;   
  16. uh = unescapeHTML;  
posted on 2009-09-28 16:54  poop  阅读(5570)  评论(0编辑  收藏  举报