2016-11-23 37 views
0

問題があります。 Javascriptでオブジェクトを扱うのは私の最初の試みですが、問題が発生します。Javascript:オブジェクト関数内のクリック関数からオブジェクト関数を呼び出す

DOM-Elementを含むオブジェクトがあり、その要素の子にクリック機能を追加したいとします。クリック機能の中で私は別のオブジェクト機能を呼びたいが、機能しない。別のオブジェクト関数を呼び出すには、this.functionname()を使用する必要がありますが、なぜなら私はクリック関数の中にいるからです。thisは私のオブジェクトではなく、私がクリック機能を追加する子要素です。だから私の質問は:どのように私のオブジェクト関数をクリック機能の内部から呼び出すかです。ここで

は私のコードは(あれば条件コードの途中では重要ではありません)です:

function ManagementCard(card) { 
    this.card = card; 
    this.row = null; 
    this.rowAlt = null; 
    this.managementCard = this; 
} 

ManagementCard.prototype.initializeCard = function() { 

    this.card.find(".card ul li div:first-child").click(function() { 
     row = $(this).parent(); 

     if(this.rowAlt != null && this.rowAlt[0] != $(this).parent()[0]) 
     { 
      this.rowAlt.children("div:last-child").collapse('hide'); 
      $(this.rowAlt).css('background-color', ''); 
      $(this.rowAlt).css('color', ''); 
      this.rowAlt = null; 
     } 
     this.toggleManagementRow(); <=== Important! Call object-function from inside of a click-function which is inside the object-function 
    }); 
} 

ManagementCard.prototype.getCard = function() { 
    return this.card; 
} 

答えて

2

別の変数、例えば内のオブジェクトへの参照をしてくださいself。これは、JavaScriptの

ManagementCard.prototype.initializeCard = function() { 
    var self = this; 
    this.card.find(".card ul li div:first-child").click(function() { 
     row = $(this).parent(); 

     if(this.rowAlt != null && this.rowAlt[0] != $(this).parent()[0]) 
     { 
      this.rowAlt.children("div:last-child").collapse('hide'); 
      $(this.rowAlt).css('background-color', ''); 
      $(this.rowAlt).css('color', ''); 
      this.rowAlt = null; 
     } 
     self.toggleManagementRow(); 
    }); 
} 
+0

はどうもありがとうございましたことを行うには:)ただ、解決策を受け入れ読み、それが – kruben

+0

簡単な解決策を一般的なことですが、私は理由を理解します:) 再度、感謝します – mic4ael

+0

は心配しないで働く理由を理解しようとしない – kruben

関連する問題