2012-09-11 114 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; 
     }); 
    } 
}