>  기사  >  웹 프론트엔드  >  초보자는 innerHTML로 얻은 콘텐츠를 어떻게 이스케이프합니까? 코드는 무엇입니까?

초보자는 innerHTML로 얻은 콘텐츠를 어떻게 이스케이프합니까? 코드는 무엇입니까?

云罗郡主
云罗郡主앞으로
2018-10-18 14:15:492408검색

本篇文章给大家带来的内容是关于新手如何对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>

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


위 내용은 초보자는 innerHTML로 얻은 콘텐츠를 어떻게 이스케이프합니까? 코드는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제