Prettify for generated string

2019-08-30 19:00发布

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" );
        });
        }
    );

1条回答
乱世女痞
2楼-- · 2019-08-30 19:24

Here is detailed answer as per documentation

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>

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>

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

查看更多
登录 后发表回答