0

私は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>
4

1 に答える 1

1

event.target.resultスクリプトを配置する前にスクリプトを適用する行を追加しました#filecontents

$(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;
      contents = hljs.highlightAuto(contents).value; // convert to highlighted
      $('#filecontents').html(contents);
    }
    reader.readAsText(file)
  });
});​

フィドルの例。

于 2012-09-16T01:39:27.010 に答える