Method 1:
Use a browser internal converter to implement conversion. The essentials are to dynamically create a container label element, such as Div, and set the string to be converted to the innertext of this element (ie supported) | textcontent (supported by Firefox), and then returns the innerhtml of this element to obtain the HTML-encoded string, which can be displayed in turn (in fact, the conversion is not needed when the display is complete, directly assign a value to the DIV to display it normally ).
<textarea id="runcode93589"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
Method 2: Regular Expression replacement
The regular expression is used to convert <> and space characters into HTML encoding. Because this method is not built-in to the system, it is easy to see that some special labels are not replaced, and the efficiency is low.
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]
You can run the test first. I also found that the first method is quite easy to use. It's really good. You must remember it.
In addition, some htmlencode functions are used by some editors. You can add them as needed. However, please note that,CodeBe sure to test it. It is really troublesome to test it when the jb51.net webmaster posts this information. It has been modified many times.Copy codeThe Code is as follows: function htmlencode (text ){
TEXT = text. Replace (/&/g ,"&");
TEXT = text. Replace (/"/g ,""");
TEXT = text. Replace (/</g, "<");
TEXT = text. Replace (/>/g, "> ");
// Text = text. Replace (// \/g ,"");
TEXT = text. Replace (/\ n/g, "<br> ");
TEXT = text. Replace (/\ t/g ,"");
Return text;
}