[英]Trying to change value of Textarea (using Skulpt)
好的,所以我正在使用Skulpt在网页上对Python进行编程。 我希望单击按钮后更改解释器中的文本。 但是,无论我如何尝试,文本区域中的代码都不会更改。 但是,如果我“警告”文本区域的值,它将弹出更改后的版本,表明该按钮有效。
我也发现了这个问题: 在jQuery中设置textarea的值,但在我的情况下,这里没有任何帮助:(
这是我尝试的方法:
<textarea id="theTextArea">print 'Hello World!'</textarea>
<div id="controls">
<button type="button" onclick="replaceCode()">Replace</button>
<button type="button" onclick="testAlert()">Alert Me</button>
</div>
<script>
function replaceCode(){
document.getElementById('theTextArea').value = "print 'Thats new code'";
};
function testAlert(){
alert(document.getElementById('theTextArea').value);
};
</script>
我也尝试过更改.innerHTML,.text,实际上没有任何东西可以替代textarea中的文本。
如果有人认为有帮助,我可以添加完整的HTML文档以及用于在线python解释器的整个Skulpt设置,以防万一它使我无法定期更改textarea的值。 但是,如果现在不需要,我不希望暂时没有代码。
您忘记了onclick
的括号。 您应该使用HTMLTextAreaElement.innerHTML
更改文本区域的内容
function replaceCode(){ document.getElementById('theTextArea').innerHTML = "print 'Thats new code'"; }; function testAlert(){ alert(document.getElementById('theTextArea').innerHTML); };
<textarea id="theTextArea">print 'Hello World!'</textarea> <div id="controls"> <button type="button" onclick="replaceCode()">Replace</button> <button type="button" onclick="testAlert()">Alert Me</button> </div>
因此事实证明,Skulpt妨碍了我在单击按钮时修改python代码。 我需要使用一个可能在Skulpt随附的导入文件之一中定义的函数。 该函数如下所示:
editor.setValue("print 'Thats new code'");
然后它实际上在textarea中改变了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.