【发布时间】:2011-04-25 08:57:17
【问题描述】:
设置
我正在尝试为我的网站制作一个基于对象的验证代码,您可以在其中将输入定义为对象并将属性附加到它,有点像这样
function input(id,isRequired) {
this.id = id
this.isRequired = isRequired
this.getElement = getElem;
this.getValue = getValue;
this.writeSpan = writeSpan;
this.checkText = checkText;
this.isText = true
this.checkEmpty = checkEmpty;
this.isEmpty = true
this.isValid = false
}
我目前有一个像这样设置的事件处理程序
firstName.getElement().onblur = function() {validate(firstName)}
其中 firstName 是输入对象,getElement() 方法执行以下操作:
function getElem() {
return document.getElementById(this.id)
}
问题
我想要做的是能够通过使用类似于.this 的东西来使用 validate 函数引用 firstName 对象,并有效地删除匿名函数。我想这样做主要是因为我正在与不太熟悉 javascript 的团队成员一起工作,并且代码越少越好。
我猜我正在寻找的代码看起来像这样:
firstName.getElement().onblur = validate
function validate() {
object = "your code here"
}
这可能吗?
【问题讨论】:
标签: javascript events validation object