An HTML string is a string with an HTML special symbol such as ' <div id= ' a ' >aklsdjfklsjdfl</div> ', which we usually have to process and then output to prevent the output from being a true HTML element, which is the < into HTML symbol code such as <.
If the string is very long and there are a lot of special symbols in it, how can we simply and efficiently turn them all around? Cycle? Regular expression? Not at all! and look:
The code is as follows |
Copy Code |
function Bian (a) {return new Option (a). InnerHTML} |
That's all! How to use:
The code is as follows |
Copy Code |
function Bian (a) {return new Option (a). InnerHTML} var a = ' <div id= ' a ' >aklsdjfklsjdfl</div> '; Alert (Bian (a)); |
By replacing special characters to implement the
1 HTML Encoding:
The code is as follows |
Copy Code |
function HTMLEnCode (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; } |
2) HTML decoding:
The code is as follows |
Copy Code |
function HtmlDecode (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; } |