我剛開始使用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