【发布时间】:2023-03-13 03:33:01
【问题描述】:
我刚开始使用 javascript,但我缺少一些我所知道的重要内容。我希望你能帮我填补空白。
所以我试图运行的脚本是假设计算文本字段中的字符,并更新一个段落以告诉用户他们输入了多少个字符。我有一个名为 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;
【问题讨论】:
标签: javascript function events methods pointers