Javascript 单击按钮将一个预标记内容复制到另一个内容
我正在写一个烧瓶网络应用程序。以下是我的一个web应用程序页面的内容 我想要的是在点击按钮时将id为“editor2”的pre标记的内容复制到“editor1”。我正在使用ace编辑器Javascript 单击按钮将一个预标记内容复制到另一个内容,javascript,html,ace-editor,Javascript,Html,Ace Editor,我正在写一个烧瓶网络应用程序。以下是我的一个web应用程序页面的内容 我想要的是在点击按钮时将id为“editor2”的pre标记的内容复制到“editor1”。我正在使用ace编辑器 {% extends "layout.html" %} {% block content %} <pre id="editor1" style="width: 600px; height: 500px; display: inline-block;"></pre> <button
{% extends "layout.html" %}
{% block content %}
<pre id="editor1" style="width: 600px; height: 500px; display: inline-block;"></pre>
<button type="button" onclick="fork()" class="btn btn-primary" style="vertical-align: middle;">Fork</button>
<pre id="editor2" style="width: 600px; height: 500px; display: inline-block;"></pre>
<p id="id">hey</p>
<script>
var editor1 = ace.edit("editor1");
editor1.setTheme("ace/theme/twilight");
editor1.getSession().setMode("ace/mode/javascript");
var editor2 = ace.edit("editor2");
editor2.setTheme("ace/theme/twilight");
editor2.getSession().setMode("ace/mode/javascript");
</script>
<script>
function fork(){
var text = document.getElementById("editor2").value;
document.getElementById("editor1").innerHTML = text;
}
</script>
{% endblock %}
{%extends“layout.html”%}
{%block content%}
叉
嘿
var editor1=ace.edit(“editor1”);
编辑1.setTheme(“ace/theme/twilight”);
editor1.getSession().setMode(“ace/mode/javascript”);
var editor2=ace.edit(“editor2”);
编辑2.setTheme(“ace/theme/twilight”);
editor2.getSession().setMode(“ace/mode/javascript”);
函数fork(){
var text=document.getElementById(“editor2”).value;
document.getElementById(“editor1”).innerHTML=text;
}
{%endblock%}
现在,当我点击按钮时,“editor1”预内容将被文本“undefined”替换,它将变为非活动状态,即我无法在其中写入。一个通用
元素
,就像您的感谢现在它没有给出undefined。但editor1变得不可编辑,即在点击按钮后,我无法再编辑它。我给你们的只是对“如何将元素内容复制到另一个元素”的回答。至于“如何使用元素中的代码并使用某个库对其进行编辑”,您必须参考库中的API。我从未见过/使用过烧瓶。我想它与烧瓶无关@Richard Denton解决方案工作正常。PS:您的解决方案非常适合我的问题标题,但我甚至无法投票,因为它需要15个声誉:/它肯定与某个库有关,因为无论@Richard Denton的代码使用什么,这肯定不是原生的DOM API或核心JavaScript:)您可能应该将特定的技术添加到问题的标题或其标记中。谢谢,它很有效,解决了我的两个问题。但是为什么@hon2a的答案中的“document.getElementById('editor1')。innerHTML=document.getElementById('editor2')。innerHTML;”会使editor1不可编辑。因为Ace编辑器不直接使用编辑器对象进行存储,所以它使用虚拟呈现器。看见
function fork() {
var text = editor2.getSession().getValue();
editor1.getSession().setValue( text );
}
document.getElementById('editor').innerHTML = document.getElementById('editor2').innerHTML;