首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何侦听Ace编辑器更改事件并作出反应

如何侦听Ace编辑器更改事件并作出反应
EN

Stack Overflow用户
提问于 2016-03-01 19:44:45
回答 1查看 18.4K关注 0票数 16

在ACE (https://ace.c9.io/#nav=api&api=editor)中使用一个简单的getValue() (当有一个on更改事件并将新文本发送到div )中,说明on更改事件是如何工作的?

EN

回答 1

Stack Overflow用户

发布于 2016-03-01 20:19:01

htp/hbxhgdr1/http://jsfiddle.net/revathskumar/rY37e/

HTML

代码语言:javascript
运行
复制
<div class="container">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">Editor</h3>
    </div>
    <div class="panel-body">
      <a href="#" onclick="update()">go</a>
      <div id="editor" onChange="update()">function foo(items) { var x = "All this is syntax highlighted"; return x; }
      </div>
    </div>
  </div>
  <div id="output">Output is here (click 'go' and write HTML and js in the editor) </div>
  <div class="text-center">---End of editor---</div>
</div>

JavaScript

代码语言:javascript
运行
复制
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
editor.getSession().on('change', function() {
  update()
});

function update() //writes in <div> with id=output
{
  var val = editor.getSession().getValue();
  var divecho = document.getElementById("output");
  divecho.innerHTML = val;
}

函数update()实现与编辑器关联的onChange事件。如果单击go-link,然后在编辑器中写入字符,则update()-function将<div>中的编辑器的内容输出为HTML (innerHTML)。

CSS

代码语言:javascript
运行
复制
#editor {
  /** Setting height is also important, otherwise the editor won’t show up **/
  height: 300px;
}

#output {
  height: 100px;
}

https://ace.c9.io/节,收听事件

也请参阅这个问题:https://stackoverflow.com/questions/23011386/ace-editor-onchange-not-working

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35732051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档