Javascript 是否可以将textarea设置为ACE编辑器?

Javascript 是否可以将textarea设置为ACE编辑器?,javascript,textarea,ace-editor,Javascript,Textarea,Ace Editor,我看到了一个类似的话题,但我找不到答案。我正在尝试将Ace编辑器连接到textarea,但未成功。然后我发现了 我更喜欢将编辑器连接到textarea,而不是div。那么,可以将Ace连接到textarea吗 提前谢谢 这取决于替换textarea后要对其执行什么操作,但使用几行js很容易做到 // create new ace instance var editor = ace.edit(document.createElement("div")) editor.session.setValu

我看到了一个类似的话题,但我找不到答案。我正在尝试将Ace编辑器连接到
textarea
,但未成功。然后我发现了

我更喜欢将编辑器连接到textarea,而不是div。那么,可以将Ace连接到textarea吗


提前谢谢

这取决于替换textarea后要对其执行什么操作,但使用几行js很容易做到

// create new ace instance
var editor = ace.edit(document.createElement("div"))
editor.session.setValue(textArea.value)
// set size
editor.container.style.height = textArea.clientHeight + "px";
editor.container.style.width = textArea.clientWidth + "px";
// replace textarea with the editor
textArea.parentNode.replaceChild(editor.container, textArea)
// trigger redraw of the editor
editor.resize()
并将编辑器替换为textarea

var value = editor.getValue()
var start = editor.session.doc.positionToIndex(editor.selection.getRange().start)
var end   = editor.session.doc.positionToIndex(editor.selection.getRange().end)
textArea.value = value
textArea.setSelectionRange(start, end)
editor.container.parentNode.replaceChild(textArea, editor.container)
editor.destroy()

您也可以尝试使用ace的textarea扩展名

不确定ace,但我知道CodeMirror具有该功能。@Mosho是的,我知道,我已经尝试了CodeMirror,但thanx仍然需要您的回答。