這裡擴充了Ext.form.HtmlEditor組件,為其添加了keyup,keydown,keypress事件監聽。重寫了Ext.form.HtmlEditor的方法:
initEditor、initComponent;
重寫後的Ext.form.HtmlEditor樣本:
/***<br /> * 重寫Ext.form.HtmlEditor,為其添加鍵盤事件<br /> * author: hoojo<br /> * email: hoojo_@126.com<br /> * blog: http://blog.csdn.net/IBM_hoojo<br /> * create by: 2010-8-14<br /> * ext-lib: 3.2.1<br /> * version: 1.0<br /> */<br />Ext.override(Ext.form.HtmlEditor, {<br />initEditor : function(){<br /> var dbody = this.getEditorBody();<br /> var ss = this.el.getStyles('font-size', 'font-family', 'background-image', 'background-repeat');<br /> ss['background-attachment'] = 'fixed'; // w3c<br /> ss['background-color'] = 'white';<br /> dbody.bgProperties = 'fixed'; // ie<br /> Ext.DomHelper.applyStyles(dbody, ss);<br /> if(this.doc){<br /> try{<br /> Ext.EventManager.removeAll(this.doc);<br /> }catch(e){}<br /> }<br /> this.doc = this.getDoc();<br /> Ext.EventManager.on(this.doc, {<br /> 'mousedown': this.onEditorEvent,<br /> 'dblclick': this.onEditorEvent,<br /> 'click': this.onEditorEvent,<br /> 'keyup': this.onEditorKeyUpEvent,<br /> 'keydown': this.onEditorKeyDownEvent,<br /> 'keypress': this.onEditorKeyPressEvent,<br /> buffer:100,<br /> scope: this<br /> });<br /> if(Ext.isGecko){<br /> Ext.EventManager.on(this.doc, 'keypress', this.applyCommand, this);<br /> }<br /> if(Ext.isIE || Ext.isSafari || Ext.isOpera){<br /> Ext.EventManager.on(this.doc, 'keydown', this.fixKeys, this);<br /> }<br /> this.initialized = true;<br /> this.fireEvent('initialize', this);<br /> this.doc.editorInitialized = true;<br /> this.pushValue();<br /> },<br />initComponent: function () {<br />this.addEvents(<br /> 'initialize',<br /> 'activate',<br /> 'beforesync',<br /> 'beforepush',<br /> 'sync',<br /> 'push',<br /> 'editmodechange',<br /> 'keydown',<br /> 'keyup',<br /> 'keypress'<br /> );<br />},<br />onEditorKeyPressEvent : function(e){<br /> this.updateToolbar();<br /> this.fireEvent("keypress", this, e);<br /> },<br /> onEditorKeyUpEvent : function(e){<br /> this.updateToolbar();<br /> this.fireEvent("keyup", this, e);<br /> },<br /> onEditorKeyDownEvent : function(e){<br /> this.updateToolbar();<br /> this.fireEvent("keydown", this, e);<br /> }<br />});</p><p>/**<br /> * 重寫後的Ext.form.HtmlEditor有了鍵盤的keyPress事件了<br /> */<br />Ext.ns("Ext.hoo.editor");<br />Ext.hoo.editor.HTMLEditor = Ext.extend(Ext.form.HtmlEditor, {<br />constructor: function () {<br />Ext.hoo.editor.HTMLEditor.superclass.constructor.call(this, {<br /> renderTo: Ext.getBody(),<br /> fieldLabel:'Biography',<br /> height:200,<br />listeners: {<br />"keydown": function (editor, e) {<br />alert("keydown:" + editor.getValue());<br />},<br />"keyup": function (editor, e) {<br />alert("keyup:" + editor.getValue());<br />},<br />"keypress": function (editor, e) {<br />alert("keypress:" + editor.getValue());<br />}<br />}<br />});<br />}<br />});
注意:要添加鍵盤事件請添加Ext.override裡的那段代碼。這段是擴充代碼,目的是為HtmlEditor添加鍵盤事件的。
html頁面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br /><html><br /> <head><br /> <title>Ext 樣本</title></p><p> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br /> <meta http-equiv="description" content="this is my page"><br /> <meta http-equiv="content-type" content="text/html; charset=gbk"><br /> <link rel="stylesheet" type="text/css" href="ext-3.2.1/resources/css/ext-all.css" /><br /> <script type="text/javascript" src="ext-3.2.1/adapter/ext/ext-base.js"></script><br /><script type="text/javascript" src="ext-3.2.1/ext-all.js"></script><br /><script type="text/javascript" src="HtmlEditor.js"></script><br /><script type="text/javascript"><br />Ext.onReady(function(){<br />new Ext.hoo.editor.HTMLEditor();<br /> });<br /></script><br /> </head></p><p> <body><br /> </body><br /></html><br />