gpt4 book ai didi

javascript - 无法读取 NULL 的属性

转载 作者:行者123 更新时间:2023-12-01 03:51:00 24 4
gpt4 key购买 nike

我有以下 html 文件,我尝试在其中使用网络 worker 突出显示一些代码:

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

关于javascript - 无法读取 NULL 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43203656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com