[英]How do I insert html code into prism code tag?
I'm using Prism to display my components in a project and it's working well when I write the code itself inside the <pre><code class="language-markup">...</code></pre>
. 我正在使用Prism在项目中显示我的组件,当我在
<pre><code class="language-markup">...</code></pre>
编写代码时,它运行良好。
Here is my codepen . 这是我的codepen 。
<pre>
<code class="language-markup">
<!-- botão simples -->
<button class="btn left">Button</button>
<!-- botão com ícone na esquerda -->
<button class="btn left"><i class="material-icons left">add</i>Button</button>
<!-- botão com ícone na direita -->
<button class="btn left"><i class="material-icons right">add</i>Button</button>
</code>
</pre>
However, now I want to grab some innerHTML and put it into the <code></code>
tag, but when I do this it doesn't work. 但是,现在我想获取一些innerHTML并将其放入
<code></code>
标记中,但是当我这样做时它不起作用。
MY SCRIPT 我的脚本
// Get all divs with the ".topic" class and returns a list of it
// @return list | array()
function getTopicList() {
// Create the list
let list = [];
// Populate the list
$('.topic').map(function() {
list.push($(this));
});
// Returns the list
return list;
}
// Set the example code inside a "code" tag
// @param topic | jquery object
// @param exampleCode | string
function setExampleCode(topic, exampleCode) {
// Replace "<" with "<"
exampleCode = exampleCode.replace(/</g, '<');
// Set the example's code inside the "code" tag
$(topic).find('code').text(exampleCode);
}
// Get the HTML code inside the div as a string
// @param topic | jquery object
function generateExampleCode(topic) {
// Get the HTML code as string
let exampleCode = $(topic).find('.example').html();
// Calls a function to append the code as a string into the div
setExampleCode(topic, exampleCode);
}
// When the page is fully loaded
$(document).ready(function() {
// Get a list of all the divs that have the ".topic" class
let topicList = getTopicList();
// Get the HTML code of all the ".topic" divs
topicList.map(generateExampleCode);
});
OUTPUT 2 输出2
Even when I try to append a <pre><code class="language-markup">...code...</code></pre>
into the div it doesn't work. 即使我尝试将
<pre><code class="language-markup">...code...</code></pre>
附加到div中也不起作用。 How can I do this? 我怎样才能做到这一点? Is there a simpler way to do this?
有更简单的方法吗?
I've found a solution: 我找到了一个解决方案:
When you insert code via js/angular/vue/etc you have to rerun Prism. 当你通过js / angular / vue / etc插入代码时,你必须重新运行Prism。
Just use the Prism.highlightAll()
function after you insert the code; 插入代码后,只需使用
Prism.highlightAll()
函数;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.