且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何让textarea成为ACE编辑器?

更新时间:2023-02-13 13:02:21

据我了解Ace的想法,你不应该做一个 textarea 强>一个王牌编辑本身。您应该创建一个额外的div并使用 .getSession()函数更新textarea。

As far as I understood the idea of Ace, you shouldn't make a textarea an Ace editor itself. You should create an additional div and update textarea using .getSession() function instead.

html

<textarea name="description"/>
<div id="description"/>

js

var editor = ace.edit("description");
var textarea = $('textarea[name="description"]').hide();
editor.getSession().setValue(textarea.val());
editor.getSession().on('change', function(){
  textarea.val(editor.getSession().getValue());
});

或只是致电

textarea.val(editor.getSession().getValue());

仅当您使用给定的textarea提交表单时。我不确定这是否是使用Ace的正确方法,但它是在 GitHub 上使用的方式。

only when you submit the form with the given textarea. I'm not sure whether this is the right way to use Ace, but it's the way it is used on GitHub.