[英]How do you use Javascript constructor methods in a jQuery statement?
I can't figure out how to use a Javascript constructor method in a jQuery .click method. 我不知道如何在jQuery .click方法中使用Javascript构造方法。 I'm trying to get a button's function to change dynamically based on a constructor. 我正在尝试使按钮的功能根据构造函数进行动态更改。 Here's the set up: 设置如下:
<button onclick="">
</button>
needs to call a method that changes depending on another button. 需要调用一个根据另一个按钮而变化的方法。 The following is my broken code: 以下是我的损坏代码:
function GloveMode (name , array) {
this.colorArray = array;
this.displaySettings = function(){
//Title
$("#displayTitle").text(this.name);
//Display Color Set
$("#displayColors").empty();
//Totally Broken
$("#upArrow").click( function(){
addColor();
});
};
this.addColor = function(){
console.log(this.colorArray.length);
};
};
I can't figure out how to get $("#upArrow").click() to call this.colorArray properly, or how to call this.addColor() in the .click() method! 我不知道如何获取$(“#upArrow”)。click()才能正确调用this.colorArray,或者如何在.click()方法中调用this.addColor()! Please help. 请帮忙。
Your Problem is that "this" means something different in each function body. 您的问题是“ this”在每个功能体中都具有不同的含义。 So save the wanted "this" to a variable eg "self" and use that. 因此,将所需的“ this”保存到变量(例如“ self”)中并使用它。
function GloveMode (name , array)
{
var self = this;
this.colorArray = array;
this.displaySettings = function()
{
//Title
$("#displayTitle").text(this.name);
//Display Color Set
$("#displayColors").empty();
//Totally Broken
$("#upArrow").click( function()
{
self.addColor();
});
};
this.addColor = function()
{
console.log(self.colorArray.length);
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.