jQuery命名空间并使用"this"

Dow*_*046 0 javascript jquery plugins

我的对象/功能范围和对某些事物的一般理解有问题.我正在调用doc中的jQuery函数,如下所示:

$("#interactiveQA .actionTile").on('click',function(){
    $(this).activeTiles("init");
});
Run Code Online (Sandbox Code Playgroud)

然后我在这里创建脚本:

(function($) {

    var methods = {
        init: function() {
            var tileClicked = $(this).attr('id');
        }
    };

    $.fn.activeTiles = function(method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.slider');
        }
    };

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

这是我想要找到的.尽可能在技术上,你能解释为什么var tileClicked = $(this)attr('id')在当前位置不起作用吗?您能否请详细解释您将采取哪些不同的做法或最佳做法等?

Ror*_*san 5

错字:

$(this).attr('id');
Run Code Online (Sandbox Code Playgroud)

请注意 .