javascript - CodeMirror getCursor() 不起作用? jQuery JavaScript

标签 javascript jquery cursor codemirror

使用 CodeMirror。我无法让 getCursor() 函数工作。我有一个附有代码镜像源的 jsFiddle。

----> see here JSfiddle <----

我正在尝试将文本插入编辑器,然后强制光标向后移动指定数量的空格。我只是想用 getCursor() 获取光标位置,但我似乎无法让它工作。有什么想法吗?

$(document).ready(function() {

//Changing the textarea to a CodeMirror rich text editor

var editor = CodeMirror.fromTextArea(document.getElementById('theZone'), {
    mode: 'text/html',
    lineWrapping : true,
    lineNumbers : true,
    extraKeys : {
    "Tab": "indentMore", 
    "Shift-Tab": "indentLess",
    "'>'": function(cm) { cm.closeTag(cm, '>'); },
    "'/'": function(cm) { cm.closeTag(cm, '/'); }
} ,
onCursorActivity: function(cm) {
    cm.setLineClass(hlLine, null, null);
    hlLine = cm.setLineClass(cm.getCursor().line, null, "activeline");
}
});


//When SELECT changes - insert the value into the CM editor, set focus, get cursor   position, move cursor back [x] amount of spaces.
$('#sel').change(function() {
    var selected = $(this).find('option:selected');
    var mynum = selected.data('val');
    editor.replaceSelection($(this).val(), focus);
    editor.focus();
    var start_cursor = editor.getCursor();  //I need to get the cursor position
    alert(start_cursor);  //Cursor position always comes up [object Object]

    //write code to move cursor back [x] amount of spaces. [x] is the data-val value.


});






});

最佳答案

该代码似乎工作得很好。 alert() 不会显示对象。使用 console.log() 代替。 我添加了其余的代码。

    $('#sel').change(function() {
    var selected = $(this).find('option:selected');
    var mynum = selected.data('val');
    editor.replaceSelection($(this).val(), focus);
    editor.focus();
    var start_cursor = editor.getCursor();  //I need to get the cursor position
    console.log(start_cursor);  //Cursor position 
    var cursorLine = start_cursor.line;
    var cursorCh = start_cursor.ch;

    //Code to move cursor back [x] amount of spaces. [x] is the data-val value.
    editor.setCursor({line: cursorLine , ch : cursorCh -mynum });

});

关于javascript - CodeMirror getCursor() 不起作用? jQuery JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17729194/

相关文章:

haskell - 如何在 Haskell Gloss 中隐藏鼠标光标

javascript - 如何刷新 TinyMCE 以获取使用 JavaScript 添加的代码

jquery - 从子 div 中追加父 div

javascript - 如何使用 jquery-i18n-properties 和 JavaScript 动态更改语言?

javascript - 如何在jquery中保留所有选择元素的选定值

java - postgresql存储过程中的结果集就像java一样

javascript - Sencha Touch 2 和.focus() : set cursor into textarea

javascript - 获取搜索框以过滤结果

javascript - Angular Modal 仅适用于 Index.html

javascript - jQuery 点击函数找不到选择器