在不选择整个编辑器的情况下为 ace 编辑器设置 Value

因此,您可以使用 setValue设置 ace 编辑器的值,但是在设置值之后,编辑器将选择编辑器的整个值。你怎么解除这个?这意味着当我将 ace 编辑器的值设置为 Hello world时,它不会突出显示 Hello world

37567 次浏览

You can use the second parameter to control cursor position after setValue

editor.setValue(str, -1) // moves cursor to the start
editor.setValue(str, 1) // moves cursor to the end

This works for me!

editor.setValue(editor.getValue(), 1);
 var prevtext = $("#editor").val();
prevtext = prevtext + "<br/>";
$("#editor").val(prevtext).blur();

You can even use clearSelection() after you do an setValue();

editor.setValue("Hello World");
editor.clearSelection(); // This will remove the highlight over the text

I've been having your same issue.

Even though you can set the second parameter to either 1 or -1, I think you should also check this: https://ace.c9.io/api/editor.html#Editor.setValue

Editor.setWrapBehavioursEnabled(Boolean enabled)

Use this right after creating the editor.

This works very well for me. The difference between this method and the one shared by a user is that the caret's position is not changed, you can move it yourself using Editor.selection.moveTo(row, column), this way the user won't experience weird caret position changes when using, say, CTRL+Z to undo an action :)

I'm not sure if editor.setValue() is a remnant from the old days or what, but the proper way to set an editor's content is

editor.session.setValue(text);

or

editor.getSession().setValue(text);

This will NOT select the text, so there's no need to do any of the things mentioned on this page.

editor.setValue() explicitly selects all (and forgets to unselect it); but there's no reason to use it.