我有一个 div,里面有一些示例代码。我想在按下按钮后执行这段代码。这是我尝试过的:
HTML
<button onclick="javascript:playcode()"> </button>
JS
function playcode () {
var execcode= "";
for (i=1; i<=countCodeLine (); i++)
execcode += document.getElementById("line"+i).innerText;
var div= document.getElementById ("mydiv");
var scr= document.createElement ('script', "");
div.innerHTML= "play() {"+execcode+"}";
div.appendChild(scr);
play();
}
但它不起作用。我怎样才能做到这一点?
更新:这里是来自控制台的错误:
(line 9: pre.appendChild(scr)) Uncaught SyntaxError: Unexpected token {
(line 10: play()) Uncaught TypeError: object is not a function