在使用代码镜像时显示警报中的文本区域内容

Display the textarea content in alert while using code mirror

本文关键字:文本 区域 代码 镜像 显示      更新时间:2023-09-26

我试图提醒文本区域的内容,该文本区域被用作按钮单击上的代码镜像。但它显示为空白。如果我删除代码镜像的脚本,它将显示内容。

我需要知道使用代码镜像有什么问题。它不能反映文本区的变化。

<textarea id="code" name="code">
</textarea>
<input type="button" onClick="func()">
    <script>
      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        lineNumbers: true
      });
function func() {
alert(document.getElementById('code').value);
}
    </script>

请帮帮我。

您需要使用editor.getValue()编程API命令从代码编辑器中获取该值。

input {font-family: 'Segoe UI';}
<!-- Create a simple CodeMirror instance -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.5.0/codemirror.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.5.0/codemirror.min.js"></script>
<textarea name="" id="myTextarea" cols="30" rows="10"></textarea>
<input type="button" value="Code?" onclick="func();">
<script>
  var editor = CodeMirror.fromTextArea(myTextarea, {
    lineNumbers: true
  });
  var func = function () {
    alert(editor.getValue());
  }
</script>