在html页面中显示html代码(Display html code in html page)

编程入门 行业动态 更新时间:2024-10-06 12:32:24
在html页面中显示html代码(Display html code in html page)

我有一个contentEditable div,我想显示这个div的内容的html脚本。 我正在使用bootbox警报来显示我的代码,并使用Rainbow插件来突出显示语法。

这是我正在使用的代码:

bootbox.alert('<pre><code data-language="html">'+ document.getElementById('editor').innerHTML + '</code></pre>');

我还使用了xmp标签和这个脚本:

<pre><![CDATA[ This is a demo, tags like <p> will appear literally. ]]></pre>

如本文所示: 如何在PRE或类似的东西中显示原始html代码,但没有转义它 ,但它们都没有工作。

这就是我得到的:

我怎么解决这个问题?

I've a contentEditable div, and I want to display the html script for the content of this div. I'm using a bootbox alert to display my code, and the Rainbow Plugin to highlight the syntax.

The is the code I'm using :

bootbox.alert('<pre><code data-language="html">'+ document.getElementById('editor').innerHTML + '</code></pre>');

I also used the xmp tag and this script:

<pre><![CDATA[ This is a demo, tags like <p> will appear literally. ]]></pre>

as shown in this article : How to display raw html code in PRE or something like it but without escaping it, but none of them has worked.

This is what I'm getting :

How can I solve this ?

最满意答案

使用实体,比如

var encodeHtmlEntity = function(str) { var buf = []; for (var i=str.length-1;i>=0;i--) { buf.unshift(['&#', str[i].charCodeAt(), ';'].join('')); } return buf.join(''); }; bootbox.alert('<pre><code data-language="html">'+ encodeHtmlEntity(document.getElementById('editor').innerHTML)) + '</code></pre>');

Use entities, like

var encodeHtmlEntity = function(str) { var buf = []; for (var i=str.length-1;i>=0;i--) { buf.unshift(['&#', str[i].charCodeAt(), ';'].join('')); } return buf.join(''); }; bootbox.alert('<pre><code data-language="html">'+ encodeHtmlEntity(document.getElementById('editor').innerHTML)) + '</code></pre>');

更多推荐

本文发布于:2023-08-03 23:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402448.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   代码   html   page   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!