How do newbies escape the content obtained by innerHTML? what is the code

云罗郡主
Release: 2018-10-18 14:15:49
forward
2462 people have browsed it

本篇文章给大家带来的内容是关于新手如何对innerHTML获得的内容转义?代码是什么,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

<body>
    <div id="code" style="display:none">
        var res = a > b ? a: b;
        res = res > c ? res : c;
        return res;
    </div>
</body>
<script>
var txt=document.getElementById("code").innerHTML;
document.write(txt);
//  输出:var res = a > b ? a: b;
//  res = res > c ? res : c;
//  return res;
//原因是用document.write输出时会对已转义的字符再次转义。
alert(txt);
//  输出:var res = a &gt; b ? a: b;
//  res = res &gt; c ? res : c;
//  return res;
var str = txt.replace(/&gt;/g,">"); 
alert(str);
//  输出:var res = a > b ? a: b;
//  res = res > c ? res : c;
//  return res;
</script>
Copy after login

以上就是对新手如何对innerHTML获得的内容转义?代码是什么的全部介绍,本文内容紧凑,希望大家可以有所收获,更多请关注PHP中文网。


The above is the detailed content of How do newbies escape the content obtained by innerHTML? what is the code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template