Home >Web Front-end >JS Tutorial >The most concise way to encode html strings in JavaScript_javascript tips

The most concise way to encode html strings in JavaScript_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:34:161074browse

html string refers to a string with html special symbols such as '

aklsdjfklsjdfl
'. We usually need to process it and then output it to prevent the output from becoming a real html element, that is, the html symbol code that changes < into <.

If the string is very long and contains many special symbols, how can we transcode them all simply and efficiently? cycle? Regular expression? No need! Let’s take a look:

Copy code The code is as follows:

function bian(a){return new Option(a).innerHTML}

That’s all!

How to use:

Copy code The code is as follows:

function bian(a){return new Option(a).innerHTML}
var a = '
aklsdjfklsjdfl
';
alert(bian(a));
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn