简体   繁体   English

尝试更改Textarea的值(使用Skulpt)

[英]Trying to change value of Textarea (using Skulpt)

Okay, so I'm using Skulpt to program Python on a webpage. 好的,所以我正在使用Skulpt在网页上对Python进行编程。 I would like the text in the interpreter change once a button is clicked. 我希望单击按钮后更改解释器中的文本。 But no matter how I try, the code in the text area doesn't change. 但是,无论我如何尝试,文本区域中的代码都不会更改。 However, if I 'alert' the value of the text area, it brings up the changed version, indicating that the button works. 但是,如果我“警告”文本区域的值,它将弹出更改后的版本,表明该按钮有效。

I've also found this question: Set value of textarea in jQuery but nothing in here helped in my case :( 我也发现了这个问题: 在jQuery中设置textarea的值,但在我的情况下,这里没有任何帮助:(

Here is how I try it: 这是我尝试的方法:

    <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>

Also I've tried changing .innerHTML, .text and nothing actually replaced the text in the textarea. 我也尝试过更改.innerHTML,.text,实际上没有任何东西可以替代textarea中的文本。

If anyone thinks it could help, I could add the full HTML document with the whole Skulpt setup for online python interpreter, in case it somehow doesn't let me change the value of the textarea in a regular way. 如果有人认为有帮助,我可以添加完整的HTML文档以及用于在线python解释器的整个Skulpt设置,以防万一它使我无法定期更改textarea的值。 But I prefer not to have a wall of code for now if it's not needed for now. 但是,如果现在不需要,我不希望暂时没有代码。

You forgot brackets in your onclick s. 您忘记了onclick的括号。 You should use HTMLTextAreaElement.innerHTML to change the textarea's content 您应该使用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> 

So it turns out Skulpt was getting in my way of modifying python code on a button click. 因此事实证明,Skulpt妨碍了我在单击按钮时修改python代码。 I needed to use a function which was defined probably in one of the imported documents which come with Skulpt. 我需要使用一个可能在Skulpt随附的导入文件之一中定义的函数。 The function looks like this: 该函数如下所示:

    editor.setValue("print 'Thats new code'");

And then it actually changes in the textarea :) 然后它实际上在textarea中改变了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM