Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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_Scope - Fatal编程技术网

Javascript 使用jQuery在函数之间保留值

Javascript 使用jQuery在函数之间保留值,javascript,jquery,scope,Javascript,Jquery,Scope,我正在编写一个jQuery插件,在变量方面遇到了一些问题。例如,我的插件有以下框架结构,它将作用于文本框。在我的init函数中,我想设置一个变量并将一个按键事件绑定到我的文本框。这个按键事件需要调用另一个函数,在这个函数中我需要变量。这可能吗 (function($){ var methods = { init : function(options){ return this.each(function(){ var

我正在编写一个jQuery插件,在变量方面遇到了一些问题。例如,我的插件有以下框架结构,它将作用于文本框。在我的init函数中,我想设置一个变量并将一个按键事件绑定到我的文本框。这个按键事件需要调用另一个函数,在这个函数中我需要变量。这可能吗

(function($){
    var methods = {
        init : function(options){
            return this.each(function(){
                var $this = $(this);
                var someVar = 'somevar';
                $this.keypress(function(event){
                    //I want to call my customFunction
                });
            });
        },
        customFunction : function(){
            //I am wanting to access 'someVar' here
        };
    $.fn.mynamespace = function(method){
        //handle which method to call here
    };
})(jQuery);

非常感谢。

在外部范围内声明
myVar
,JavaScript中的函数可能是允许您使用的:

(function($){
    var someVar;
    var methods = {
        init : function(options){
            return this.each(function(){
                var $this = $(this);
                someVar = 'somevar';
                $this.keypress(function(event){
                    //I want to call my customFunction
                });
            });
        },
        customFunction : function(){
            //You may access 'someVar' here
        };
    $.fn.mynamespace = function(method){
        //handle which method to call here
    };
})(jQuery);
编辑:但是,如果要存储特定于每个HTML元素的数据,则应使用jQuery对象的方法或元素本身的方法。例如:

(function($){
    var someVar;
    var methods = {
        init : function(options){
            return this.each(function(){
                var $this = $(this);
                $this.data('myCode.blargle','somevar');
                $this.keypress(function(event){
                    //I want to call my customFunction
                });
            });
        },
        customFunction : function(){
          var someVar = $(this).data('myCode.blargle');
          // or $.data(this,'myCode.blargle');
        };
    $.fn.mynamespace = function(method){
        //handle which method to call here
    };
})(jQuery);

在外部作用域中声明
myVar
,JavaScript中的函数可能是允许您工作的:

(function($){
    var someVar;
    var methods = {
        init : function(options){
            return this.each(function(){
                var $this = $(this);
                someVar = 'somevar';
                $this.keypress(function(event){
                    //I want to call my customFunction
                });
            });
        },
        customFunction : function(){
            //You may access 'someVar' here
        };
    $.fn.mynamespace = function(method){
        //handle which method to call here
    };
})(jQuery);
编辑:但是,如果要存储特定于每个HTML元素的数据,则应使用jQuery对象的方法或元素本身的方法。例如:

(function($){
    var someVar;
    var methods = {
        init : function(options){
            return this.each(function(){
                var $this = $(this);
                $this.data('myCode.blargle','somevar');
                $this.keypress(function(event){
                    //I want to call my customFunction
                });
            });
        },
        customFunction : function(){
          var someVar = $(this).data('myCode.blargle');
          // or $.data(this,'myCode.blargle');
        };
    $.fn.mynamespace = function(method){
        //handle which method to call here
    };
})(jQuery);

我想过这样做。例如,var someVar是文本框中输入的字符的串联。现在,如果我有两个使用这个插件的文本框,那么someVar将是两个文本框的串联。i、 只有当我有一个使用插件的文本框时,这才有效。我调用函数是否正确:$this.mynamespace('customFunction')?我想过这样做。例如,var someVar是文本框中输入的字符的串联。现在,如果我有两个使用这个插件的文本框,那么someVar将是两个文本框的串联。i、 只有当我有一个使用插件的文本框时,这才有效。我调用函数是否正确:$this.mynamespace('customFunction')?