简体   繁体   English

将本地数据保存在JQuery函数中

[英]Saving a local data in a JQuery function

I want to create two JQuery functions. 我想创建两个JQuery函数。 One of which will use the data created in the other function. 其中一个将使用在另一个函数中创建的数据。 Say: 说:

jQuery.fn.myScroller = function (child) {
    var $moveable = this.children(child);
    var $children = this.children(child).children();

    posl = posl.replace('px', '');
    post = post.replace('px', '');
    var varList = [];
    $children.each(function () {
        var currVar = {};
        currVar.w = $(this).width();
        varList.push(currVar);
    });
    // I want to save varList variable somehow.
    this.varList = varList; // (1)
};

jQuery.fn.scrollUnitLeft = function () {
    // Now I need to use this varList if this function called
    // But the following returns undefined.
    // However, I've saved it inside this at (1)
    console.log(this.varList)
// More code...
}


$('#main_div').myScroller("#dummydiv");
$('#main_div').scrollUnitLeft();

As I explained in comments in the code, this doesn't work. 正如我在代码注释中所解释的那样,这是行不通的。

How can I do this? 我怎样才能做到这一点?

Creating a namespace or global variable as suggested doesn't look clean to me. 按照建议创建名称空间或全局变量对我来说并不干净。 You're already extending jQuery, so make it a jQuery variable: 您已经在扩展jQuery,因此使其成为jQuery变量:

jQuery.fn.varList = varList;

Edit: I don't really know the jQuery internals. 编辑:我真的不知道jQuery内部。 If fn is meant to be for functions only, either put it into jQuery itself or write a getter 如果fn仅用于函数,则将其放入jQuery本身或编写一个getter

jQuery.fn.getVarList = function () {
    return varList;
}

You can create a namespace at the global scope: 您可以在全局范围内创建名称空间:

window.yourNamespace = {};

and then put your varList variable in that namespace: 然后将您的varList变量放在该命名空间中:

yourNamespace.varList = []; //Whatever you want here

This way the variable will be available to both of your functions (or any function for that matter). 这样,变量将可用于您的两个函数(或与此相关的任何函数)。

Your understanding of this looks a little buggy to me (pardon me, you may be a lot more experienced than me)! this理解对我来说似乎有点不足(对不起,您可能比我更有经验)! this refers to the caller of the function. this是指函数的调用者。 Both the times it is the div main_div . 两次都是div main_div Try using a global variable which is declared outside both the functions or use data attribute on the div in the first function, and access that value in the second. 尝试使用在两个函数外部都声明的全局变量,或者在第一个函数中使用div上的data属性,然后在第二个函数中访问该值。

When you run your jQuery functions, this refers to the jQuery object instance used in each case. 当您运行jQuery函数时, this是指在每种情况下使用的jQuery对象实例。

In your code you create two jQuery object instances (one for every $(...) call), so the data is set in the first instance, and hence not available to the second instance. 在您的代码中,创建两个jQuery对象实例(每个$(...)调用一个),因此数据在第一个实例中设置,因此第二个实例不可用。

If you were to run both your methods on the same jQuery object instance, they would work as expected. 如果要在同一个jQuery对象实例上运行两个方法,则它们将按预期工作。

jQuery.fn.mySet = function(){
  this.myVar = this.attr('id');
};

jQuery.fn.myGet = function(){
  console.log(this.myVar);
}

$('#a_div').mySet();
$('#a_div').myGet(); // outputs 'undefined'

var $other_div = $('#other_div');
$other_div.mySet();
$other_div.myGet(); // outputs 'other_div'

To achieve what you intend, you have to persist the data someplace other than the jQuery object instance. 为了实现您想要的目标,您必须将数据持久保存在jQuery对象实例之外的其他地方。 jQuery offers a way to do this via the .data() method. jQuery提供了一种通过.data()方法执行此操作的方法。 This method allows you to attach data to DOM elements. 此方法使您可以将数据附加到DOM元素。 Check its documentation . 检查其文档

jQuery.fn.myRealSet = function(){
  this.data('myVar', this.attr('id'));
};

jQuery.fn.myRealGet = function(){
  console.log(this.data('myVar'));
}

$('#final_div').myRealSet();
$('#final_div').myRealGet(); // outputs 'final_div'

You can test this snippets here: http://jsfiddle.net/HPjYn/ 您可以在以下位置测试此代码段: http : //jsfiddle.net/HPjYn/

EDIT: I cannot yet comment on other people's answers, but adding a jQuery prototype varable as suggested will make the data available from any jQuery object instance, which I don't think is what it's intended here. 编辑:我尚无法评论其他人的答案,但是按照建议添加jQuery原型变量将使数据可从任何jQuery对象实例获得,我认为这不是这里的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM