0

Greetings fellow stackoverflowers,

I'm working on a jQuery class where I have a function that appends some html code to a container.

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>');
    }
}

So far, no problem. In this same class I have another function

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

I want the appended HTML to call this function setProperty function when I click on it. How can I do this?

For what's it worth, I initialize my class like this

 var myVariable = new MyPlugin();

I know I could do <a href="#" onclick="myVariable.setProperty('', '')"> but the plugin can't know the variable.

Any help is appreciated!

4

1 に答える 1

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;
        });
     }
}
于 2012-09-11T19:41:42.817 に答える