简体   繁体   English

防止执行html代码

[英]prevent html code from being executed

I am making a webpage, and I have some javascript code for a countdown timer. 我正在制作一个网页,并且有一些用于倒数计时器的javascript代码。 below the timer, I want to show people the code for the project without forcing them to open developer tools, so I made the following: 在计时器下方,我想向人们展示该项目的代码,而不必强迫他们打开开发人员工具,因此我做了以下工作:

<body onload="show()">
    <span id="showCode">
        <!-- where I want my code to show -->
    </span>
    <script>

    function show() {
        document.getElementById('showCode').innerHTML = document.body.innerHTML;
    }

    </script>
</body>

How can I use javascript to show the inner html of an element instead of executing the code? 如何使用javascript显示元素的内部html而不执行代码?

"You need to convert your < and > characters to HTML entities like so:" “您需要像这样将<>字符转换为HTML实体:”

<pre class="prettyprint">
  &lt;script&gt;
    $(document).ready(function(){
      setTimeout(function(){
        console.log('deleting cookie');
        $.cookie('cookie',null,{domain: document.domain});
      },1000);
    });
  &lt;/script&gt;
</pre>

"I would also recommend wrapping the code in <code> tags in addition to the existing <pre> tags." “除了现有的<pre>标签外,我还建议将代码包装在<code>标签中。”

~ Travis 特拉维斯

Display javascript as code snippet 将JavaScript显示为代码段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM