[英]Cannot read property of NULL
我有以下html文件,其中我嘗試使用網絡工作者突出顯示一些代碼:
<link rel="stylesheet" href="./highlight/styles/default.css"> <script src="./highlight/highlight.pack.js"></script> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script> if (typeof(Worker) !== "undefined") { addEventListener('load', function() { var code = document.querySelector('#code'); var worker = new Worker('worker.js'); worker.onmessage = function(event) { code.innerHTML = event.data; } worker.postMessage(code.textContent); }) } else { } </script> <pre><code> // This is a generated file with many packages `ifdef MACRO_1 `else package pkg_1; typedef logic [1:0] t; typedef enum t { IDLE = 2'd0 , ARMED = 2'd1 , WRITE = 2'd2 , BUSY = 2'd3 } e; </code></pre>
但是我收到一條錯誤消息:Uncaught TypeError:無法讀取null的屬性'textContent'(worker.postMessage(content.textContent));
有解決方案嗎?
在代碼元素上添加id =“ code”屬性: https : //jsbin.com/daqijafoca/edit?html,輸出
<link rel="stylesheet" href="./highlight/styles/default.css">
<script src="./highlight/highlight.pack.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
if (typeof(Worker) !== "undefined") {
addEventListener('load', function() {
var code = document.querySelector('#code');
var worker = new Worker('worker.js');
worker.onmessage = function(event) { code.innerHTML = event.data; }
worker.postMessage(code.textContent);
})
} else {
}
</script>
<pre><code id="code">
// This is a generated file with many packages
`ifdef MACRO_1
`else
package pkg_1;
typedef logic [1:0] t;
typedef enum t {
IDLE = 2'd0
, ARMED = 2'd1
, WRITE = 2'd2
, BUSY = 2'd3
} e;
</code></pre>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.