2012-09-11 35 views
0

問候同胞stackoverflowers,jQuery的類:通過調用追加的HTML功能的類

我工作的一個jQuery類在那裏我有一些附加的HTML代碼到容器的功能。

this.displayOptions = function(property, container) { 
    for (var i = 0; i < this[property + 'Count']; i++) { 
     $(container).append('<a href="#" onclick="' + call_a_function_from_this_class + '; return false"></a>'); 
    } 
} 

到目前爲止,沒有問題。 同樣在這一類我有另一個功能

this.setProperty = function(property, index) { 
    this[property] = index; 
    this.update(); 
} 

我想要的附加HTML調用這個函數setProperty功能,當我點擊它。 我該怎麼做?

對於什麼是值得,我初始化我的課像這樣

var myVariable = new MyPlugin(); 

我知道我可以做<a href="#" onclick="myVariable.setProperty('', '')">,但該插件無法知道該變量。

任何幫助表示讚賞!

回答

0

找到我的解決方案。我得到它這樣工作:

this.displayOptions = function(property, container) { 
    var self = this; 
    for (var i = 0; i < this[property + 'Count']; i++) { 
     var element = $('<a href="" data-property="' + property + '" />'); 
     $(container).append(element); 

     element.click(function() { 
      self.setProperty($(this).attr('data-property'), $(this).index()); 
     return false; 
     }); 
    } 
}