无法读取NULL的属性 [英] Cannot read property of NULL
本文介绍了无法读取NULL的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下html文件,我试图用web worker强调一些代码:
< link rel =stylesheethref =./ 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> //这是生成的文件包含许多包``ifdef MACRO_1`elsepackage pkg_1; typedef逻辑[1:0] t; typedef enum t {IDLE = 2'd0,ARMED = 2'd1,WRITE = 2'd2,BUSY = 2'd3} e;< / code>< / pre>
$
但是我得到一个错误:Uncaught TypeError:无法读取属性'textContent'为null(worker.postMessage( content.textContent));
是否有解决方案?
解决方案在代码元素上添加id =code属性: https://jsbin.com/daqijafoca/编辑?html,输出
< link rel =stylesheethref =./ 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){
$ b 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>
//这是一个包含许多软件包的生成文件
`ifdef MACRO_1
`else
package pkg_1;
typedef逻辑[1:0] t;
typedef enum t {
IDLE = 2'd0
,ARMED = 2'd1
,WRITE = 2'd2
,BUSY = 2'd3
} e;
< / code>< / pre>
I have the following html file in which I try to highlight some code using a web 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>
But I get an error saying: Uncaught TypeError: Cannot read property 'textContent' of null (worker.postMessage(content.textContent));
Is there a solution?
解决方案Add id="code" attribute on code element: 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>
这篇关于无法读取NULL的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文