本文介绍了文档元素上的onkeydown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下方法:


function grid(){

this._el = document.createElement(" TABLE");


var self = this;

document.addEventListener(" onkeydown",function(event)

{self.gridKeyDown (事件);},false);

}


grid.prototype.gridKeyDown = function(event){

/ /一些代码

}


var datagrid = new grid();

addEventListener没有被添加, gridKeyDown方法永远不会执行
。我可以在文档对象上添加onkeydown吗?有关于mozilla dom的好参考吗?

I''m trying the following:

function grid() {
this._el = document.createElement("TABLE");

var self = this;
document.addEventListener("onkeydown", function(event)
{self.gridKeyDown(event);}, false);
}

grid.prototype.gridKeyDown = function(event) {
//some code
}

var datagrid = new grid();
the addEventListener doesn''t get added, the gridKeyDown method never
gets executed. Can I add onkeydown to the document object? Is there a
good reference about mozilla dom?

推荐答案




如果你这样做,自我将不会被onkeydown定义

调用handler,因为onkeydown将在网格对象构造函数的

范围之外调用 - 这是所需的行为。



If you do it this way, self will not be defined by the time onkeydown
handler is called, because onkeydown will be called outside of the
scope of the grid object constructor -- which is the desired behavior.








这篇关于文档元素上的onkeydown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 22:50