gpt4 book ai didi

JavaScript : How to get reference to the parent object inside a child object

转载 作者:行者123 更新时间:2023-12-02 20:32:31 24 4
gpt4 key购买 nike

我有以下代码,其中我无法获取对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com