无法读取 NULL 的 属性

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: Cannot read 属性 'textContent' of null (worker.postMessage(content.textContent));

有解决办法吗?

在代码元素上添加 id="code" 属性:https://jsbin.com/daqijafoca/edit?html,output

<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>