关闭库键事件监听不起作用

时间:2012-12-11 05:55:54

标签: javascript google-closure

我尝试了以下代码,使用google closure-library监听关键事件,但代码不起作用。

goog.require('goog.events.EventHandler');
goog.require('goog.events.EventType');
goog.require('goog.dom');
goog.require('goog.editor.Command');
goog.require('goog.editor.Field');
goog.require('goog.editor.Field.EventType');

-----------------------below--not worked------------------------------------------

var myField = new goog.editor.Field('editMe');

goog.events.listen(myField, goog.events.EventType.KEYPRESS,
         function(){alert('12')});

1 个答案:

答案 0 :(得分:3)

你必须在相关的DOM元素上听取事件:

goog.events.listen(myField.getElement(), goog.events.EventType.KEYPRESS, function(){alert('12')});