我正在用 HTML 和 JavaScript 制作 Python 编辑器,CodeMirror 用于代码缩进、行号和语法着色,而 Skulpt 用于执行。
我有一些代码在其中引用了外部脚本(http://www.skulpt.org/static/skulpt.min.js
):
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript">
</script>
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript">
</script>
<script src="skulpt-stdlib.js" type="text/javascript">
</script>
<script src="codemirror.js" type="text/javascript">
</script>
<script src="python.js" type="text/javascript">
</script>
<link href="codemirror.css" rel="stylesheet" type="text/css">
<title></title>
</head>
<body>
<script type="text/javascript">
var editor;
function outf(text) {
var mypre = document.getElementById("dynamicframe");
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 = editor.getDoc().getValue();
var mypre = document.getElementById("dynamicframe");
mypre.innerHTML = '';
Sk.pre = "dynamicframe";
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());
});
}
//<![CDATA[
window.onload = function() {
editor = CodeMirror.fromTextArea(document.getElementById('textbox'), {
mode: {
name: "python",
version: 2,
singleLineStringErrors: false
},
lineNumbers: true,
indentUnit: 4
});
} //]]>
</script>
<textarea id="textbox" name="textbox"></textarea>
<br>
<button onclick="runit()" type="button">Run</button>
<pre id="dynamicframe"></pre>
<div id="canvas"></div>
</body>
</html>
我希望所有脚本都存储在本地(不包括 jQuery),所以我复制http://www.skulpt.org/static/skulpt.min.js
到本地服务器并调用它skulpt.min.js
。然后我改变了这一行:
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript">
</script>
至...
<script src="skulpt.min.js" type="text/javascript">
</script>
现在代码在控制台中提出了这个错误:
Uncaught ReferenceError: Sk is not defined
在 HTML 的第 38 行:
Sk.pre = "dynamicframe";
为什么会发生这种情况,我该如何解决?