0

我正在通过常见问题解答中提到的google-code-prettifyCDN 使用这里 是突出显示代码的功能。现在我有问题<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script> prettyPrint()

prettyPrint()当我调用此函数时,它说未定义 cdn 在页面加载并突出显示语法时自动调用此函数,但加载后我正在更改代码内容,然后我希望它再次突出显示。但我无法做到这一点prettyPrint()

我自己托管google-code-prettify,然后我使用prettyPrint()它并且工作正常但是当我在更改代码后调用此函数时没有任何反应,它不会突出显示我的语法

这是我正在使用的代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Developer Query</title>
<link href="css/header.css" rel="stylesheet" type="text/css">
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<style>
/*This CSS applied button in which section user is visiting. */
ul#nav > li#btn2 {
    border-top: 3px solid #FF9900;
    border-left: 2px solid grey;
    border-right: 2px solid grey;
    border-bottom: 2px solid white;
    border-radius: 4px;
}
/*Button CSS is end. */ 
.content{
    width:1136px;
    margin:0px auto;
}
input#question_title{
    margin:20px auto;
    width:100%;
    height:20px;
}
textarea#question_content{
    width:100%;
    height:300px;
    margin:0px auto;
}
#preview{
    width:100%;
    overflow:auto;
}
</style>
</head>

<body>
<?php include_once('php_include/header.php'); ?>
<div class="content">
    <input type="text" name="question_title" id="question_title" placeholder="What happed with your code?" />
    <textarea id="question_content" name="question_content" placeholder="Elabrote your code" onKeyUp="render();"></textarea>
    <h3>Here's what it look like</h3>
    <code class="prettyprint"><div id="preview">&lt;html&gt;</div></code>
    </div>
</body>
<script>
//HELPER FUNCTIONS////
function $(id){
    return document.getElementById(id);
}
function render(){//
    var question_content = $("question_content").value;
    //Sanitizing data//
    var entitles = {//List of all Html entitiles
        '<':"&lt;",
        '>':"&gt;",
        '\n':"<br>",        
    }
    question_content = question_content.replace(/<|>|\n/gi, function (html_ent){return entitles[html_ent];});
    var preview = $("preview");
    preview.innerHTML = question_content;   
    //prettyPrint();
}
</script>
</html>
4

1 回答 1

0

运行时prettyprint(),它将“prettyprinted”类添加到它更改的每个元素。如果您需要重新申请prettyprint(),请先删除“prettyprinted”类,然后调用prettyprint().

于 2016-05-05T23:02:29.923 回答