The simplest way to encode HTML strings in JavaScript
- 2020-03-30 04:05:23
- OfStack
HTML strings are '< Div id = "a" > Aklsdjfklsjdfl< / div> 'such a string with an HTML special symbol, we usually have to process it before we output it so that it doesn't turn out to be a real HTML element. Become & amp; Lt; Such HTML symbol code.
If strings are long and have a lot of special symbols in them, how can we simply and efficiently transcode them all? Cycle? Regular expressions? Don't have to! And look:
function bian(a){return new Option(a).innerHTML}
That's all!
Usage:
function bian(a){return new Option(a).innerHTML}
var a = '<div id="a">aklsdjfklsjdfl</div>';
alert(bian(a));