我刚开始使用JavaScript,我错过了一些重要的知识。我希望你能帮我填补这个空白。如何在JavaScript中运行对象的方法onEvent?
所以我试图运行的脚本是假设要计算文本字段中的字符,并更新段落以告诉用户他们输入了多少个字符。我有一个名为charCounter的对象。 sourceId是要对字符进行计数的文本区域的ID。statusId是每次按下某个键时要更新的段落的ID。
function charCounter(sourceId, statusId) {
this.sourceId = sourceId;
this.statusId = statusId;
this.count = 0;
}
有一种叫做updateAll的方法。它更新字符数并更新段落。
charCounter.prototype.updateAll = function() {
//get the character count;
//change the paragraph;
}
我有一个启动函数,当窗口加载时调用。
function start() {
//This is the problem
document.getElementbyId('mytextfield').onkeydown = myCounter.updateAll;
document.getElementbyId('mytextfield').onkeyup = myCounter.updateAll;
}
myCounter = new charCounter("mytextfield","charcount");
window.onload = start;
上面的代码是问题所在。为什么在事件触发时,我无法调用myCounter.updateAll方法?这真让我感到困惑。我明白,如果你调用方法likeThis()你会从函数中获得一个值。如果你叫它likeThis你会得到一个指向函数的指针。我将我的事件指向一个函数。我也尝试过直接调用这个函数,它工作得很好,但是当事件被触发时它不起作用。
我错过了什么?
感谢您的所有答案。这里有三种不同的实现。
实现1
function CharCounter(sourceId, statusId) {
this.sourceId = sourceId;
this.statusId = statusId;
this.count = 0;
};
CharCounter.prototype.updateAll = function() {
this.count = document.getElementById(this.sourceId).value.length;
document.getElementById(this.statusId).innerHTML = "There are "+this.count+" charactors";
};
function start() {
myCharCounter.updateAll();
document.getElementById('mytextfield').onkeyup = function() { myCharCounter.updateAll(); };
document.getElementById('mytextfield').onkeydown = function() { myCharCounter.updateAll(); };
};
myCharCounter = new CharCounter('mytextfield','charcount');
window.onload = start;
实现2
function CharCounter(sourceId, statusId) {
this.sourceId = sourceId;
this.statusId = statusId;
this.count = 0;
};
CharCounter.prototype.updateAll = function() {
this.count = document.getElementById(this.sourceId).value.length;
document.getElementById(this.statusId).innerHTML = "There are "+ this.count+" charactors";
};
CharCounter.prototype.start = function() {
var instance = this;
instance.updateAll();
document.getElementById(this.sourceId).onkeyup = function() {
instance.updateAll();
};
document.getElementById(this.sourceId).onkeydown = function() {
instance.updateAll();
};
};
window.onload = function() {
var myCounter = new CharCounter("mytextfield","charcount");
myCounter.start();
};
实现3
function CharCounter(sourceId, statusId) {
this.sourceId = sourceId;
this.statusId = statusId;
this.count = 0;
};
CharCounter.prototype.updateAll = function() {
this.count = document.getElementById(this.sourceId).value.length;
document.getElementById(this.statusId).innerHTML = "There are "+this.count+" charactors";
};
function bind(funcToCall, desiredThisValue) {
return function() { funcToCall.apply(desiredThisValue); };
};
function start() {
myCharCounter.updateAll();
document.getElementById('mytextfield').onkeyup = bind(myCharCounter.updateAll, myCharCounter);
document.getElementById('mytextfield').onkeydown = bind(myCharCounter.updateAll, myCharCounter);
};
myCharCounter = new CharCounter('mytextfield','charcount');
window.onload = start;
谢谢!这为我的问题提供了很多亮点! – user182666 2009-10-01 19:37:50
不客气@dgendill! – CMS 2009-10-03 18:28:02