Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery名称空间和使用;这";_Javascript_Jquery_Plugins - Fatal编程技术网

Javascript jQuery名称空间和使用;这";

Javascript jQuery名称空间和使用;这";,javascript,jquery,plugins,Javascript,Jquery,Plugins,我的对象/功能范围和对某些东西的一般理解有问题。我在doc ready中调用jQuery函数,如下所示: $("#interactiveQA .actionTile").on('click',function(){ $(this).activeTiles("init"); }); 然后,我创建脚本,如下所示: (function($) { var methods = { init: function() { var tileClicke

我的对象/功能范围和对某些东西的一般理解有问题。我在doc ready中调用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);
以下是我想要了解的内容。请尽可能从技术上解释为什么var tileClicked=$(this)attr('id')在其当前位置不起作用?您还可以在技术上详细说明您将采取的不同做法或最佳做法等吗?

打字错误:

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

打字错误:

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

注意

如果
init
中的
this
已经是一个jQuery对象,也许您不应该使用
$(this)
,而是简单地编写
this
,如下所示:

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

如果您的
this
init
中已经是一个jQuery对象,那么您可能不应该使用
$(this)
,而是简单地编写
this
,如下所示:

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

这通常是我尝试设置插件构造函数的方式:

$.fn.whatever = function (method) {
    var args = arguments;
    var argss = Array.prototype.slice.call(args, 1);

    return this.each(function () {
        $this = $(this);
        if (methods[method]) {
            methods[method].apply($this, argss);
        }
        else if (typeof method === "object" || !method) {
            methods.init.apply($this, args);
        }
        else {
            $.error("Method " + method + " does not exist on jQuery.whatever");
        }
    });
};

我不确定它是否能解决您的问题,但我在过去没有遇到过问题…

这通常是我尝试设置插件构造函数的方式:

$.fn.whatever = function (method) {
    var args = arguments;
    var argss = Array.prototype.slice.call(args, 1);

    return this.each(function () {
        $this = $(this);
        if (methods[method]) {
            methods[method].apply($this, argss);
        }
        else if (typeof method === "object" || !method) {
            methods.init.apply($this, args);
        }
        else {
            $.error("Method " + method + " does not exist on jQuery.whatever");
        }
    });
};

我不确定它是否能解决您的问题,但我在过去没有遇到过问题…

您的意思是
$(this.attr('id'),对吗?是的,我更新了问题。这个输入错误并不是问题所在,因为我为堆栈QT重新输入了它。代码似乎工作正常:@David-谢谢你指出这一点。我在我的代码中做了一些与我发布到这个堆栈中的代码稍有不同的事情,它帮助我意识到不是我的$(这个)导致了这个问题,对吗?是的,我更新了问题。这个输入错误并不是问题所在,因为我为堆栈QT重新输入了它。代码似乎工作正常:@David-谢谢你指出这一点。我在我的代码中做了一些与我发布到这个堆栈中的代码稍有不同的事情,它帮助我意识到不是我的$(这个)导致了这个问题。更新的问题,但没有解决堆栈。THX更新了问题,但无法解决堆栈。thxIt可以双重包装jQuery对象,如果我这样做的话。attr('id')我遇到一个JS错误-uncaught TypeError:Object#没有方法'attr',可以双重包装jQuery对象,如果我这样做的话。attr('id'))我遇到了一个JS错误-uncaughttypeerror:Object#没有“attr”方法。你能给我一两条评论,解释一下为什么你喜欢用这种方法编写$.fn吗?谢谢,嗯,我真的忘记了为什么我要区分
args
argss
,但是使用
返回这个
允许在原始jQuery中选择多个元素,并且每个元素都应用了您的插件。例如,如果您希望选择具有类“testing class”的所有元素,并将您的插件应用于所有元素(例如,工具提示),那么您将使用
$(“.testing class”).testPlugin({“a”:“b”})
,您需要
返回此。每个
。这有意义吗?这也是为了继续jQuery提供的链接。你能给我一两条评论来解释为什么你喜欢用这种方法来编写$.fn吗?谢谢,嗯,我真的忘记了为什么我要区分
args
argss
,但是使用
返回这个
允许在原始jQuery中选择多个元素,并且每个元素都应用了您的插件。例如,如果您希望选择具有类“testing class”的所有元素,并将您的插件应用于所有元素(例如,工具提示),那么您将使用
$(“.testing class”).testPlugin({“a”:“b”})
,您需要
返回此。每个
。这有意义吗?还需要继续jQuery提供的链接。