Javascript - 将文本复制到CodeMirror文本框内部

时间:2016-09-01 19:06:05

标签: javascript html codemirror

我正在尝试CodeMirror文本框,使用按钮将文本从一个文本字段插入另一个文本字段。添加了标准文本框插入文本,但它不适用于CodeMirror。我该怎么做CodeMirror?

var editor = CodeMirror.fromTextArea(CodeMirrorText, {
  lineNumbers: true
});

/* FOR CodeMirror TEXTBOX */ 
function appendText(elem, text) {
  var oldVal = $(elem).val();
  if (oldVal) oldVal += "\n";
  $(elem).val(oldVal + text);
}

$(function () {
  $("#opt button").not("#send").click(function () {
    appendText("#CodeMirrorText", $(this).text());
  });
  $("#send").click(function () {
    appendText("#CodeMirrorText", $("#small").val());
  });
});

/* FOR CLASSIC TEXTBOX */ 
function appendText(elem, text) {
  var oldVal = $(elem).val();
  if (oldVal) oldVal += "\n";
  $(elem).val(oldVal + text);
}

$(function () {
  $("#opt button").not("#send").click(function () {
    appendText("#big", $(this).text());
  });
  $("#send").click(function () {
    appendText("#big", $("#small").val());
  });
});
#big {
  height:100px; width:400px;
}
#small {
  height:25px; width:300px;
}
#myTextarea {
  height:25px; width:300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="Lib/Plugins/CodeMirror/codemirror.js"></script>
<link href="Lib/Plugins/CodeMirror/codemirror.css" rel="stylesheet"/>
<div id="opt">

  <button>Option 1</button>
  <button>Option 2</button>
  <button>Option 3</button>
  <button>Option 4</button>
  <button>Option 5</button>
  <textarea id="small"></textarea>
  <button id="send">Send</button>
</div>
Classic Textbox: (working)<br/>
<textarea id="big"></textarea><link rel="stylesheet" href="lib/codemirror.css">
<hr/>With CodeMirror: (not working)<br/>
<textarea id="CodeMirrorText" name="CodeMirrorText"></textarea>

0 个答案:

没有答案