1

我有两个简单的 Python 代码文件hello.pyworld.py.

print("hello")
print("world")

基于这种方式,我使用 Prism.js 和 Ajax 编写了一个 HTML 文件,该文件按如下helloworld.htm方式格式化和呈现。hello.pyworld.py

<!doctype html>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/prism.min.js></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/plugins/line-numbers/prism-line-numbers.min.js></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/components/prism-python.min.js></script>
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/themes/prism.min.css>
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/plugins/line-numbers/prism-line-numbers.min.css>
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/prism/1.25.0/themes/prism-coy.min.css>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
    function btnclick(_url){
        $.ajax({
            url : _url,
            type : 'post',
            success: function(data) {
             $('#DIVID').html(data);
            },
            error: function() {
             $('#DIVID').text('An error occurred');
            }
        });
    }
</script>
<div style="width:50%;height:100%;position:fixed;top:0;left:0">
<a href="cursor:pointer" onclick="btnclick('hello.py')">hello.py</a><br>
<a href="cursor:pointer" onclick="btnclick('world.py')">world.py</a>
<div style="width:50%;height:100%;position:fixed;top:0;left:50%">
<pre class=line-numbers><code class=lang-py><div id="DIVID"><?php include('hello.py');?></div></code></pre>

<code class=lang-py>不幸的是,这个 HTML 文件在最后一行不能正常工作。虽然<code>没有<code class=lang-py>解决这个问题,但我仍然想使用 Prism.js 来格式化代码文件。我该如何解决这个问题?

PS似乎这个问题与How to Re-Run Prism.js on AJAX Content有关Prism.highlightAll();需要与Ajax一起运行,虽然我还没有找到正确的位置Prism.highlightAll();

4

0 回答 0