ExternalError:TypeError:使用Skulpt runit()函数无法读取null的属性“innerHTML”

时间:2016-08-10 07:12:16

标签: javascript jquery python html skulpt

我得到了:

ExternalError: TypeError: Cannot read property 'innerHTML' of null on line 2
来自此代码的控制台日志中的

...

<!DOCTYPE html>
<html>
    <head>
    <link href='https://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.js"></script>
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript"></script> 
<script src="http://www.skulpt.org/static/skulpt-stdlib.js" type="text/javascript"></script> 
        <title>Python Coding Area</title>
        <script type='text/javascript'>
function outf(text) { 
    var mypre = document.getElementById("output"); 
    mypre.innerHTML = mypre.innerHTML + text; 
} 
function builtinRead(x) {
    if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
            throw "File not found: '" + x + "'";
    return Sk.builtinFiles["files"][x];
}
function runit() { 
   var prog = document.getElementById("textbox").value; 
   var mypre = document.getElementById("dynamicframe"); 
   mypre.innerHTML = ''; 
   Sk.pre = "output";
   Sk.configure({output:outf, read:builtinRead}); 
   (Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'canvas';
   var myPromise = Sk.misceval.asyncToPromise(function() {
       return Sk.importMainWithBody("<stdin>", false, prog, true);
   });
   myPromise.then(function(mod) {
       console.log('success');
   },
       function(err) {
       console.log(err.toString());
   });
} 
        </script>
    </head>
    <body style="font-family:Raleway !important;">
        <textarea id="textbox" cols="50" rows="10"></textarea><br /> 
<button type="button" onclick="runit()">Run</button> 
</form> 
        <pre id="dynamicframe" ></pre> 
<div id="canvas"></div> 
    </body>
</html>

当我单击运行按钮以运行带有skulpt的python代码时。在这种情况下使用skulpt有问题吗?我从skulpt网站(skulpt.org)中获取了一些代码,然后稍微改了一下。为什么我会收到错误,如何阻止错误?

1 个答案:

答案 0 :(得分:0)

您的网页中不存在“mypre”元素。

You should add a condition like this: 
var mypre = document.getElementById("output"); 

if( mypre ) != null){
    mypre.innerHTML = mypre.innerHTML + text; }