/** jquery html Encoding, decoding* principle is the use of jquery's own HTML () and text () functions can escape HTML characters * Virtual a div by assigning values and values to get the desired Html encoding or decoding */< Script src= "http://libs.baidu.com/jquery/1.9.0/jquery.js" ></script><script type= "Text/javascript" >//html encoding gets the HTML escape entity function HtmlEncode (value) { return $ (' <div/> '). Text (value). html (); HTML decoding gets the HTML entity function HtmlDecode (value) { return $ (' <div/> '). HTML (value). text ();} </script>
<script type= "Text/javascript" >//get HTML escape character function HtmlEncode (HTML) { return document.createelement (' a ' ). AppendChild ( document.createtextnode (HTML)). parentnode.innerhtml;};/ /Get HTML function HtmlDecode (HTML) { var a = document.createelement (' a '); a.innerhtml = html; return a.textcontent;}; </script>
Encoded function Html_encode (str) { var s = ""; if (Str.length = = 0) return ""; s = str.replace (/&/g, ">"); s = S.replace (/</g, "<"); s = S.replace (/>/g, ">"); s = s.replace (//g, ""); s = s.replace (/\ '/g, "'"); s = s.replace (/\ "/g," "" "); s = S.replace (/\n/g, "<br>"); return s; } Decode function Html_decode (str) { var s = ""; if (Str.length = = 0) return ""; s = str.replace (/>/g, "&"); s = S.replace (/</g, "<"); s = S.replace (/>/g, ">"); s = s.replace (//g, ""); s = s.replace (/'/g, "\ '"); s = s.replace (/"/g," \ ""); s = S.replace (/<br>/g, "\ n"); return s; }