私は2つのファイル.jsとhtmlファイルを持っています。ファイルを読み取ることができます。コードは表示されますが、コードの色は変更されません。私はそれをテストしましたが、ハードコーディングするとhighlight.pack.jsが機能しています。エラーは $(#filecontents).html(contents) にあると思われます。この問題を解決する方法がわかりません。プラグインは、 highlightcodeからダウンロードできます
.js ファイル
$(document).ready(function(){
$('#fileform input:file').change(function(event){
file = event.target.files[0];
reader = new FileReader();
reader.onload = function(event) {
var contents = event.target.result;
$('#filecontents').html(contents);
}
reader.readAsText(file)
});
});
.html ファイル
<link rel="stylesheet" href="styles/school_book.css">
<script src="highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<form id="fileform" action="" method="post"> <input type="file" name="file" /></form>
<pre><code class = "python"><p id="filecontents"></p></code></pre>