javascript - jQuery 命名空间和使用 "this"

标签 javascript jquery plugins

我在对象/函数范围和对某些事物的一般理解方面遇到了问题。我正在准备好文档中调用 jQuery 函数,如下所示:

$("#interactiveQA .actionTile").on('click',function(){
    $(this).activeTiles("init");
});

然后我像这样创建脚本:

(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);

这是我想要找出的内容。从技术上来说,您能否解释一下为什么 vartileClicked = $(this)attr('id') 在当前位置不起作用?您能否在技术细节上解释一下您会采取哪些不同的做法或最佳实践等?

最佳答案

打字错误:

$(this).attr('id');

注意

关于javascript - jQuery 命名空间和使用 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160674/

相关文章:

jquery - 如何使用 jquery(ajaxForm 插件)将表单提交到外部站点,但不重新加载到其他页面?

javascript - 如何以递归方式从嵌套 JSON 中删除所有空数组 childs []

jquery - 我可以将 ASP.NET 放在 Js 文件中吗?

jQuery 拖放 ul 和 li 元素

jquery - 循环插件拒绝滚动,不知道为什么

javascript - jQuery 自定义插件回调 - 新问题

javascript - 如何在保持响应速度的同时将 YouTube 视频居中?

javascript - 如何将两个 json 对象连接在一起?

javascript - JS 绑定(bind)选项数据失败

jQuery 未在本地主机页面上加载