我刚开始使用 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;
最佳答案
我认为您在 updateAll
函数上访问实例成员时遇到问题,因为您将它用作事件处理程序,上下文(this
关键字)是触发事件的 DOM 元素,而不是您的 CharCounter 对象实例。
你可以这样做:
function CharCounter(sourceId, statusId) {
this.sourceId = sourceId;
this.statusId = statusId;
this.count = 0;
}
CharCounter.prototype.updateAll = function() {
var text = document.getElementById(this.sourceId).value;
document.getElementById(this.statusId).innerHTML = text.length;
};
CharCounter.prototype.start = function() {
// event binding
var instance = this; // store the current context
document.getElementById(this.sourceId).onkeyup = function () {
instance.updateAll(); // use 'instance' because in event handlers
// the 'this' keyword refers to the DOM element.
};
}
window.onload = function () {
var myCharCounter = new CharCounter('textarea1', 'status');
myCharCounter.start();
};
检查上面的例子运行 here .
关于javascript - 如何在 javascript 中运行对象的 onEvent 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1505624/