在tinyMce中触发keyPressEvent

时间:2012-03-19 08:24:39

标签: javascript tinymce moodle

我在Moodle(电子学习)中定制tinyMCE。我添加了一个工具栏按钮,将焦点设置到文本区域并在其中添加两个美元符号。 我需要的是在这些标志之间放置光标,以便用户可以在它们之间开始输入。 可能最好的办法就是按照左箭头编程,不是吗?但我无法弄清楚如何做到这一点。 这是代码:

tinyMCE.init({
mode : "textareas",
theme : "advanced",
theme_advanced_buttons1 : "mybutton,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,link,unlink",
theme_advanced_buttons2 : "",
theme_advanced_buttons3 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
plugins : 'inlinepopups',
setup : function(ed) {
    // Add a custom button
    ed.addButton('mybutton', {
        title : 'My button',
        image : 'img/example.gif',
        onclick : function() {
            ed.focus();
            ed.selection.setContent('$$');
        }
    });
}

}); 感谢

2 个答案:

答案 0 :(得分:2)

这应该按照你的意愿行事:

ed.addButton('mybutton', {
    title : 'My button',
    image : 'img/example.gif',
    onclick : function() {  
        ed.focus();
        ed.selection.setContent('$<span id="my_marker">\u200b</span>$');
        var $marker = $(ed.getBody()).find('#my_marker');
        ed.selection.select($marker.get(0));
        $marker.remove();
    }
});

答案 1 :(得分:0)

您可以使用以下代码段触发按键事件。

var e = jQuery.Event('keypress');
e.keyCode = 37; //Left arrow keycode 
$(document).trigger(e);

用法可能类似于

onclick : function() {
    ed.focus();
    ed.selection.setContent('$$');
    var e = jQuery.Event('keypress');
    e.keyCode = 37; //Left arrow keycode 
    $(document).trigger(e);
}