作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码,其中我无法获取对 OnKeyUp 函数内父对象的引用。据我所知,在 OnKeyUp 方法中,“this”指的是文本框。但是我如何访问父对象以便 var textboxID 获得正确的值?
function $MyObject() {
this.Control = {
inputBox: "#inputBox1",
name: "Control1",
BindEvent: function () {
$(this.inputBox).keyup(this.OnKeyUp);
},
OnKeyUp: function () {
var textBoxID = this.inputBox;
alert(textBoxID);
}
}
}
$(document).ready(function () {
var object1 = new $MyObject();
object1.Control.BindEvent();
});
最佳答案
function $MyObject() {
var self = this.Control = {
inputBox: "#inputBox1",
name: "Control1",
BindEvent: function () {
$(self.inputBox).keyup(self.OnKeyUp);
},
OnKeyUp: function () {
var textBoxID = self.inputBox;
alert(textBoxID);
}
};
}
关于JavaScript : How to get reference to the parent object inside a child object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3844309/
我是一名优秀的程序员,十分优秀!