首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >nicEdit :向编辑器中添加键(按下up\up\down)事件

nicEdit :向编辑器中添加键(按下up\up\down)事件
EN

Stack Overflow用户
提问于 2013-03-13 15:12:37
回答 4查看 5.7K关注 0票数 4

我使用nicEdit来拥有一个RTF文本区域,我需要添加一个onkeypressonkeyuponkeydown事件。

我创建我的实例如下:

代码语言:javascript
运行
复制
var emailRtf = new nicEditor({  iconsPath : 'nicEdit/nicEditorIcons.gif', 
    buttonList : ['bold','italic','underline','fontSize','forecolor','ol','ul','link','unlink','removeformat'],
    maxHeight: 600}).panelInstance('REPLY_MESSAGE');

我尝试了以下方法(使用keypresskeydownkeyup):

代码语言:javascript
运行
复制
emailRtf.addEvent("keypress", function() { alert('test') }); // Not working
emailRtf.addEvent("keypress", function(evt) { alert('test') }); // Not working

然而,以下工作:

代码语言:javascript
运行
复制
emailRtf.addEvent("blur", function() { alert('test') }); // Alert shows when I leave focus on the textArea

如何将key(press|up|down)添加到nicEdit编辑器中?

注意事项:我的页面中有RTF文本区域的多个实例,我只需要将key(press|down|up)事件添加到其中一个。我发现了问题,它将事件添加到所有实例中。,我也想让nicEdit.js完好无损.

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-14 09:50:55

看看这里,限制WYSIWYG编辑器(NicEdit)中的字符数

它应该在不改变nicedit.js的情况下工作。

代码语言:javascript
运行
复制
$("div.nicEdit-main").keyup(function () { });

看看jsfiddle http://jsfiddle.net/jGLRn/15/

票数 5
EN

Stack Overflow用户

发布于 2013-03-20 21:47:28

这里的官方文件。它向您展示了如何将(它们支持的)事件绑定到您的nicEdit实例而不使用jQuery。

代码语言:javascript
运行
复制
var commentNicEditor = new nicEditor().panelInstance('comment');
commentNicEditor.addEvent("blur", function () {
    // Sent when an editor instance loses focus    
});
commentNicEditor.addEvent("focus", function () {
    // Send when an editor gains focus (IE someone clicks inside it)    
});
commentNicEditor.addEvent("key", function () {
    // When the user presses a shortcut key (Such as control-b)    
});
commentNicEditor.addEvent("add", function () {
    // Fired when a new instance is added    
});
commentNicEditor.addEvent("panel", function () {
    // Fired when the toolbar is initialized for new instances (This is the preferred event if you want to add a function when NicEdit instances are created)    
});
票数 3
EN

Stack Overflow用户

发布于 2013-03-14 00:13:08

经过一天的研究,我做不到不修改nicEdit.js。然而,我能够做的变化很少。在这里(我使用的是0.9r24版本):

我添加了一个名为customKeyDownFunction的配置选项。默认情况下,它什么也不做。

代码语言:javascript
运行
复制
var nicEditorConfig = bkClass.extend({
    buttons : {
        'bold' : {name : __('Click to Bold'), command : 'Bold', tags : ['B','STRONG'], css : {'font-weight' : 'bold'}, key : 'b'},
        'italic' : {name : __('Click to Italic'), command : 'Italic', tags : ['EM','I'], css : {'font-style' : 'italic'}, key : 'i'},
        'underline' : {name : __('Click to Underline'), command : 'Underline', tags : ['U'], css : {'text-decoration' : 'underline'}, key : 'u'},
        'left' : {name : __('Left Align'), command : 'justifyleft', noActive : true},
        'center' : {name : __('Center Align'), command : 'justifycenter', noActive : true},
        'right' : {name : __('Right Align'), command : 'justifyright', noActive : true},
        'justify' : {name : __('Justify Align'), command : 'justifyfull', noActive : true},
        'ol' : {name : __('Insert Ordered List'), command : 'insertorderedlist', tags : ['OL']},
        'ul' :  {name : __('Insert Unordered List'), command : 'insertunorderedlist', tags : ['UL']},
        'subscript' : {name : __('Click to Subscript'), command : 'subscript', tags : ['SUB']},
        'superscript' : {name : __('Click to Superscript'), command : 'superscript', tags : ['SUP']},
        'strikethrough' : {name : __('Click to Strike Through'), command : 'strikeThrough', css : {'text-decoration' : 'line-through'}},
        'removeformat' : {name : __('Remove Formatting'), command : 'removeformat', noActive : true},
        'indent' : {name : __('Indent Text'), command : 'indent', noActive : true},
        'outdent' : {name : __('Remove Indent'), command : 'outdent', noActive : true},
        'hr' : {name : __('Horizontal Rule'), command : 'insertHorizontalRule', noActive : true}
    },
    iconsPath : '../nicEditorIcons.gif',
    buttonList : ['save','bold','italic','underline','left','center','right','justify','ol','ul','fontSize','fontFamily','fontFormat','indent','outdent','image','upload','link','unlink','forecolor','bgcolor'],
    iconList : {"bgcolor":1,"forecolor":2,"bold":3,"center":4,"hr":5,"indent":6,"italic":7,"justify":8,"left":9,"ol":10,"outdent":11,"removeformat":12,"right":13,"save":24,"strikethrough":15,"subscript":16,"superscript":17,"ul":18,"underline":19,"image":20,"link":21,"unlink":22,"close":23,"arrow":25},
    customKeyDownFunction : function() { }  
});

我修改了keyDown函数。

代码语言:javascript
运行
复制
keyDown : function(e,t) {
    this.ne.options.customKeyDownFunction();

    if(e.ctrlKey) {
        this.ne.fireEvent('key',this,e);
    }
}

在创建nicEditor实例时,我定义了customKeyDownFunction

代码语言:javascript
运行
复制
var emailRtf = new nicEditor({  
        iconsPath : 'nicEdit/nicEditorIcons.gif', 
        buttonList : ['bold','italic','underline','fontSize','forecolor','ol','ul','link','unlink','removeformat'],
        maxHeight: 600,
        customKeyDownFunction: function() { alert('key down!'); }
    }).panelInstance('REPLY_MESSAGE');

如果不需要任何自定义函数,就不要定义customKeyDownFunction

代码语言:javascript
运行
复制
var emailRtf = new nicEditor({  
        iconsPath : 'nicEdit/nicEditorIcons.gif', 
        buttonList : ['bold','italic','underline','fontSize','forecolor','ol','ul','link','unlink','removeformat'],
        maxHeight: 600
    }).panelInstance('REPLY_MESSAGE');

我只能这么做了。如果有人有更好的方法,请告诉我!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15389157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档