美化生成的字符串 [英] Prettify for generated string
问题描述
当试图选择下拉框时,我试图对生成的字符串应用修饰词语法突出显示.如果预先放置了文本,则效果很好,但是如果生成了文本,则不会突出显示文本.我试过调用prettyPrint()函数,但仍然无法正常工作.
I am trying to apply prettify syntax highlighting to a generated string when the dropdown box is selected. It works fine if the text was placed before-hand, but if it is generated, the text is not highlighted. I've tried calling prettyPrint() function but it still does not work.
$('#db').change(
function(){
query = "";
query = "<pre class=\"prettyprint\" id=\"query\">Insert Into ";
query = query + $('#db').val() + "</pre>";
document.getElementById("pp").innerHTML = query;
prettyPrint();
$.ajax({
url: "functions.php?&f=table",
type: "GET",
data: { db: $('#db').val() }
})
.done(function(result) {
$('#table').html(result);
})
.fail(function() {
alert( "error" );
});
}
);
推荐答案
此处是根据文档
如果加载此脚本,则无需调用prettyPrint()
函数,它将自动对类prettyprint
If you load this script then you don't need to call the prettyPrint()
function it will automatically prettify the content with class prettyprint
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
但是,如果分别加载css和js文件,则需要调用prettyPrint()
But if you load the css and js files separately then you need to call prettyPrint()
<link href="prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify.js"></script>
来自文档:,然后在页面加载完成后运行prettyPrint函数.一种方法是通过onload处理程序:
From documentation: and then run the prettyPrint function once your page has finished loading. One way to do this is via the onload handler thus:
<body onload="prettyPrint()">
演示 ,其中包含脚本,而不是单独加载
DEMO with script included rather than loading separate
这篇关于美化生成的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!