Set Value for ace editor without selecting the whole editor

☆樱花仙子☆ 提交于 2019-12-04 14:56:12

问题


So you can set value of an ace editor with setValue but after setting the value, the editor will select the whole value of the editor. How do you disable this? This mean when I set value of ace editor to Hello world, it won't highlight Hello world


回答1:


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



回答2:


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

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



回答3:


This works for me!

editor.setValue(editor.getValue(), 1);



回答4:


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 :)




回答5:


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


来源:https://stackoverflow.com/questions/18614169/set-value-for-ace-editor-without-selecting-the-whole-editor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!