0

我想知道如何使这段代码工作:http: //jsfiddle.net/LBXVd/2/。它几乎是干净的 jqueryboilerplate,我对这部分代码感兴趣:

Plugin.prototype = {

    init: function() {

        /// create element <a> with click function
        myEl = $('<a/>', {  
            href: '#',
            text: '###',
           click: function(e){this._handler();}
    });
     $('ul').before(myEl);
    },


    //handler for click events on my <a> element...
    _handler: function(el, options) {
        alert("my handler called");
    }
};

单击创建的元素后如何调用函数_handler?

谢谢!

4

1 回答 1

3

您的 jsfiddle 不是很有帮助,因为它没有显示任何结果……但是我认为这是因为thisthis._handler()调用中不是您所想的(它是<a>元素而不是原型对象)。

尝试

init: function() {

    var self = this; // ****

    /// create element <a> with click function
    myEl = $('<a/>', {  
        href: '#',
        text: '###',
       click: function(e){self._handler();} // ****
});
于 2013-07-18T11:50:06.180 回答