文档元素上的onkeydown事件 [英] onkeydown event on document element

查看:86
本文介绍了文档元素上的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?

推荐答案

b。*** @ gmx.net 写道:
我正在尝试以下方法:

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

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


grid.prototype.gridKeyDown = function(event){
// some代码




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


目前有关于事件处理的其他讨论以及

如何动态地引起它。


最简单的方法这样做也是最交叉兼容的:


document.onkeydown = function(event){

event = event || window.event;

//找出它应用于哪个网格对象

//然后在其上激活你的gridKeyDown函数

myGrid。 gridKeyDown(event);

}

//构造函数

函数grid(){

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

返回此内容;

}


//请注意,您需要在你可能之前定义原型

//改变它

grid.prototype = {

gridKeyDown:function(event){

//一些代码

}

}


var datagrid = new grid();


---------


每次创建
$ b $时都不需要附加onKeyDown处理程序b grid因为处理程序将处理整个

文档的keyDown事件,而不是每个单独的网格对象。

var self = this;
document.addEventListener (onkeydown,功能(事件)
{self.gr idKeyDown(event);},false);
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?
Other discussions are currently going regarding event handling and how
to dynamically cause it.

The simplest way to do it is also the most cross-compatible:

document.onkeydown = function ( event ) {
event = event || window.event;
// figure out which of your grid objects it applies to
// then fire your gridKeyDown function thereon
myGrid.gridKeyDown( event );
}
// the constructor
function grid( ) {
this._el = document.createElement("TABLE");
return this;
}

// note that you need to define the prototype before you may
// change it
grid.prototype = {
gridKeyDown: function (event) {
//some code
}
}

var datagrid = new grid();

---------

You do not need to attach the onKeyDown handler every time you create a
grid because the handler will be handling keyDown events for the entire
document, not for each individual grid object.
var self = this;
document.addEventListener("onkeydown", function(event)
{self.gridKeyDown(event);}, false);




如果你这样做,自我将不会被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.


addEventListener(" k * eydown",...

addEventListener中的所有事件名称都没有on,但是obj.onkeydown

(带有on)。

显然,一些英语语法爱好者(以及最终开发人员的帽子)是在Mozilla团队工作的
: - )


" self"是当前窗口的窗口属性。

所以

var self = this;

等于

var windows.self = windows.self;

这是错误的,毫无意义。


做出这些修改并回来;-)

addEventListener("k*eydown", ...

All event names in addEventListener are w/o "on", but obj.onkeydown
(with "on").
Obviously some English grammar lover (and end-developers hatter) is
working in the Mozilla team :-)

"self" is a window property reffering to the current window.
So
var self = this;
equal to
var windows.self = windows.self;
which is wrong and pointless.

Make these changes and come back ;-)


Random写道:
如果你这样做,自己将不会被onkeydown
处理程序的时间定义,因为onkeydown将在网格对象构造函数的范围之外调用 - 这是所需的行为。


当他应该写的时候:如果你这样做的话,自我将不会按照你期望的那样定义onkeydown处理程序被调用,因为onkeydown会被调用
在网格对象构造函数的范围之外 - 这是
期望的行为。相反,自[[VK指出]将是对当前窗口的引用。
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.
When he should have written: If you do it this way, self will not be defined as you expect by the
time onkeydown handlers is called, because onkeydown will be called
outside of the scope of the grid object constructor -- which is the
desired behavior. Instead, self [as VK pointed out] will be a
reference to the current window.






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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆