I have the following code:
var myObj = {
inputs: document.getElementsByTagName('input'),
attachKeyEvent: function() {
for ( var i = 0; i < this.inputs.length; i++ ) {
this.inputs[i].onkeypress = this.getChar;
console.log(this); // => returns ref to myObj
}
},
getChar: function(e) {
console.log(this); // => [Object HTMLInputElement]
// get a reference to myObj
}
}
我有一个带有几个
<input type="text" />
元素的 DOM 结构。 我需要编写一些方法来增强按键事件。如何在 getChar()
内获得对象实例的引用?
getChar
函数。 - Sebas