admin管理员组文章数量:1289497
I'm using nicEdit to have a RTF textarea and I need to add a onkeypress
, onkeyup
or onkeydown
event.
I create my instance like this:
var emailRtf = new nicEditor({ iconsPath : 'nicEdit/nicEditorIcons.gif',
buttonList : ['bold','italic','underline','fontSize','forecolor','ol','ul','link','unlink','removeformat'],
maxHeight: 600}).panelInstance('REPLY_MESSAGE');
I tried the following (with keypress
, keydown
and keyup
):
emailRtf.addEvent("keypress", function() { alert('test') }); // Not working
emailRtf.addEvent("keypress", function(evt) { alert('test') }); // Not working
However, the following works:
emailRtf.addEvent("blur", function() { alert('test') }); // Alert shows when I leave focus on the textArea
How can I add key(press|up|down)
to a nicEdit editor?
NOTE: I have multiple instance of RTF textarea in my page and I need to add the key(press|down|up)
event to only one. I found this question, which add the event on all instances. Also, I would like to leave nicEdit.js intact.
I'm using nicEdit to have a RTF textarea and I need to add a onkeypress
, onkeyup
or onkeydown
event.
I create my instance like this:
var emailRtf = new nicEditor({ iconsPath : 'nicEdit/nicEditorIcons.gif',
buttonList : ['bold','italic','underline','fontSize','forecolor','ol','ul','link','unlink','removeformat'],
maxHeight: 600}).panelInstance('REPLY_MESSAGE');
I tried the following (with keypress
, keydown
and keyup
):
emailRtf.addEvent("keypress", function() { alert('test') }); // Not working
emailRtf.addEvent("keypress", function(evt) { alert('test') }); // Not working
However, the following works:
emailRtf.addEvent("blur", function() { alert('test') }); // Alert shows when I leave focus on the textArea
How can I add key(press|up|down)
to a nicEdit editor?
NOTE: I have multiple instance of RTF textarea in my page and I need to add the key(press|down|up)
event to only one. I found this question, which add the event on all instances. Also, I would like to leave nicEdit.js intact.
4 Answers
Reset to default 5Take a look here Limit the number of characters in a WYSIWYG Editor (NicEdit)
it should work without changes in nicedit.js
$("div.nicEdit-main").keyup(function () { });
Look at jsfiddle http://jsfiddle/jGLRn/15/
Read the official documentation here. It shows you how to bind (their supported) events to your nicEdit instance without using jQuery:
var mentNicEditor = new nicEditor().panelInstance('ment');
mentNicEditor.addEvent("blur", function () {
// Sent when an editor instance loses focus
});
mentNicEditor.addEvent("focus", function () {
// Send when an editor gains focus (IE someone clicks inside it)
});
mentNicEditor.addEvent("key", function () {
// When the user presses a shortcut key (Such as control-b)
});
mentNicEditor.addEvent("add", function () {
// Fired when a new instance is added
});
mentNicEditor.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)
});
After a day of research, I couldn't do it without modifying nicEdit.js. However, I was able to do with very few changes. Here they are (I am using version 0.9r24):
I added one config option named customKeyDownFunction
. By default it does nothing.
var nicEditorConfig = bkClass.extend({
buttons : {
'bold' : {name : __('Click to Bold'), mand : 'Bold', tags : ['B','STRONG'], css : {'font-weight' : 'bold'}, key : 'b'},
'italic' : {name : __('Click to Italic'), mand : 'Italic', tags : ['EM','I'], css : {'font-style' : 'italic'}, key : 'i'},
'underline' : {name : __('Click to Underline'), mand : 'Underline', tags : ['U'], css : {'text-decoration' : 'underline'}, key : 'u'},
'left' : {name : __('Left Align'), mand : 'justifyleft', noActive : true},
'center' : {name : __('Center Align'), mand : 'justifycenter', noActive : true},
'right' : {name : __('Right Align'), mand : 'justifyright', noActive : true},
'justify' : {name : __('Justify Align'), mand : 'justifyfull', noActive : true},
'ol' : {name : __('Insert Ordered List'), mand : 'insertorderedlist', tags : ['OL']},
'ul' : {name : __('Insert Unordered List'), mand : 'insertunorderedlist', tags : ['UL']},
'subscript' : {name : __('Click to Subscript'), mand : 'subscript', tags : ['SUB']},
'superscript' : {name : __('Click to Superscript'), mand : 'superscript', tags : ['SUP']},
'strikethrough' : {name : __('Click to Strike Through'), mand : 'strikeThrough', css : {'text-decoration' : 'line-through'}},
'removeformat' : {name : __('Remove Formatting'), mand : 'removeformat', noActive : true},
'indent' : {name : __('Indent Text'), mand : 'indent', noActive : true},
'outdent' : {name : __('Remove Indent'), mand : 'outdent', noActive : true},
'hr' : {name : __('Horizontal Rule'), mand : '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() { }
});
I modified the keyDown function.
keyDown : function(e,t) {
this.ne.options.customKeyDownFunction();
if(e.ctrlKey) {
this.ne.fireEvent('key',this,e);
}
}
When I create my nicEditor instance, I define customKeyDownFunction
.
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');
If you don't need any custom function, simply don't define customKeyDownFunction
.
var emailRtf = new nicEditor({
iconsPath : 'nicEdit/nicEditorIcons.gif',
buttonList : ['bold','italic','underline','fontSize','forecolor','ol','ul','link','unlink','removeformat'],
maxHeight: 600
}).panelInstance('REPLY_MESSAGE');
That's the best I could do. If someone has a better way, please tell!
or you can try this way:
$('body').on("keyup", ".nicEdit-main", function(){
//Do Something...
}
本文标签: javascriptnicEditAdd key(pressupdown) events to editorStack Overflow
版权声明:本文标题:javascript - nicEdit : Add key(press|up|down) events to editor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741472085a2380667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论