日期:2014-05-16 浏览次数:20397 次
<script> var code = '<table width="100"><tr><td>我爱你</td></tr></table>'.replace(/(\<)/g, " < "); code = code.replace(/(\>)/g, "> "); code = code.replace(/(\")/g, "" "); alert(code); </script>
------解决方案--------------------
function ToHtmlEnCode(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; } function ToHTMLDeCode(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; } var html = '<table width="100">&<tr><td>我爱你</td></tr></table>' html = ToHtmlEnCode(html) alert(html) html = ToHTMLDeCode(html) alert(html)