2014-10-12 35 views
0

我有一個代表PhotoFile的類。它包含屬性,並且可以添加img元素。Javascript - 使用靜態變量添加點擊監聽器

它還包含一個靜態變量,它爲每個img元素(PhotoFile類)創建一個唯一的css級數(如「.no-1」,名爲PhotoFile.count的變量)。

var PhotoFile = function(){ 

    /*attributes */ 
    PhotoFile.count = PhotoFile.count+1;  
    this.specificClass = ".no-" + PhotoFile.count; 

    /*event listener*/ 
    this.addClickEvents = function(){ 

     $("p.listener" + this.specificClass).click(function(){ 

     console.log("onClick on " + this.SpecificClass + " will fire " + "img" + this.SpecificClass); 
     $("img " + this.specificClass).hide(); 
     }); 
    } 
} 

我嘗試附加一個jQuery點擊事件,我註冊它與唯一的當前數量。

如果輸出像這樣的點擊(5 - 是PhotoFiles的頁面數):

onClick on .no-5 will fire img.no-5 
onClick on .no-5 will fire img.no-5 
onClick on .no-5 will fire img.no-5 

但是,當觸發該事件,它總是與最後一個號碼執行的情況下,我假設它使用PhotoFile.count的最後一個值觸發事件。

如何使用獨特的css類將單擊事件添加到該類的每個對象?

我想當發射元素時,它總是需要最終的靜態變量。我應該如何綁定元素,以便事件將採用當前類的靜態變量?

回答

0

沒有看到代碼的一些上下文或工作小提琴他看起來像關閉問題。你想要綁定點擊一個單獨的函數來捕獲值。背景請參見here