我已经在CodeMirror的textarea中编写了基本代码,现在在执行编写的代码时遇到问题。
以下是HTML部分:
<form id="preview-form" method="post" action="<?php echo
$_SERVER['PHP_SELF']; ?>">
<textarea class="codemirror-textarea" name="preview-form-comment"
id="preview-form-comment">
#include<stdio.h>
int main()
{
printf("Hello World!!");
}
</textarea>
<input type="test" name="testID" id="testID" value="aqsa shahid">
<br>
<input type="submit" name="preview-form-submit" id="preview-form-
submit" value="Submit" onclick="test();">
</form>
<div id="preview-comment"></div>
这是执行代码的功能。
function test() {
//code goes here
alert(document.getElementById("preview-form-comment").value);
var code = $(".codemirror-textarea")[0];
var editor = CodeMirror.fromTextArea(code, {
lineNumbers: true,
matchBrackets: true,
mode: "text/x-csrc",
theme: "dracula"
});
CodeMirror.runMode(document.getElementById("preview-form-
comment").value, "text/x-csrc", document.getElementById('preview-
comment')); };
最佳答案
我知道您可能已经解决了这个问题,而且它已经很老了,但是如果有人在寻找答案,我会把这个问题解决掉
我用一个iframe然后在js中解决了这个问题
iframe.src = "data:text/html;charset=utf-8," + encodeURI(new_textarea.value)
对我有用。
关于codemirror - 如何执行用Codemirror textarea编写的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47208129/