简体   繁体   English

如何在jQuery语句中使用Javascript构造函数方法?

[英]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.

相关问题 使用JavaScript和sinon如何监视构造函数中调用的方法? - Using JavaScript and sinon How do you spy on methods called in the constructor? 如何使用在构造函数中创建的方法来更新同一构造函数中的另一个变量? - How do you use methods created within a constructor to update another variable within the same constructor? 你如何在if语句中使用javascript中的搜索方法? - How do you use the search method in javascript in an if statement? 如何在jQuery / javascript中正确使用if if else语句? - How do I properly use an if then else statement with jQuery/javascript? Facebook Javascript如何使用JQuery在加载时显示变量 - Facebook Javascript How do you use JQuery to show the variable on load 您如何使用javascript / jQuery重写亚马逊会员链接? - How do you use javascript/jQuery to rewrite amazon affiliate links? 如何将 PHP 字符串传递给 Javascript/JQuery 以在 function 中使用? - How do you pass PHP string to Javascript/JQuery to use in function? 您如何将SinonJS用于回调方法? - How do you use SinonJS for callback methods? 您如何同时使用“ and”和“ or”制作javascript“ if”语句? - How do you make a javascript “if” statement with both “and” and “or”? 如何将方法添加到javascript对象并使用该对象的变量 - How do you add methods to a javascript object and use the object's variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM