jQuery - 在函数之间共享变量 [英] jQuery - sharing vars between functions

查看:98
本文介绍了jQuery - 在函数之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该相当简单;我很确定我不理解它。

This should be fairly simple; i'm pretty sure I've just not understood it.

仅供参考,网站使用jQuery在内容上运行ajax .load()。

FYI, the website is using jQuery to run ajax .load() on content.

在我的主母页面上,在标题中我有我的nav .load()jQuery代码: -

On my main parent page, in the header I have my nav .load() jQuery code:-

<script type="text/javascript" charset="utf-8">

    $(document).ready(function(){

        $('a.nav').click(function() {
            page = $(this).attr('page');
            projID= $(this).attr('projID');

            if($("#mainRight").is(":visible")) { $('#mainRight').hide(200); }

            switch(page) {

            case 'projSettings': $("#mainRight").load("content.php?load=" + page, { projID: projID}); break;
            case 'projMetrics': $("#mainRight").load("content.php?load=" + page, { projID: '5000227' }); break;
            case 'projTags': $("#mainRight").load("content.php?load=" + page, { projID: projID}); break;
            case 'projShare': $("#mainRight").load("content.php?load=" + page, { projID: '5000227' }); break;

            }
            $('#mainRight').show(300);

        });

现在,基本上我希望将projID重新记忆r使用其他不提供它的a.nav点击。

Now, basically I want the projID to be rememebred for use by other a.nav clicks that don't supply it.

调用此链接的锚看起来像< a page =projSettings projID =522class =nav>设置< / a> 我想保存522.所以,其他请求(没有projID,如数据已加载到DOM中以用于其他导航,并且他们不知道 projID。我希望这是有道理的。

An linked anchor calling this looks like <a page="projSettings" projID="522" class="nav">Settings</a> and I'd it to save that 522. So, other requests (that don't have the projID, as the data has been loaded into the DOM for other navs and they don't know the projID. I hope that makes sense.

所以,我想知道如何拥有像这样的链接< a page =projSettingsclass =当前一次调用中已经使用了projID时,nav>设置< / a>

So, I'd like to know how to have links like <a page="projSettings" class="nav">settings</a> when the projID has already been used in the previous call.

有什么想法吗?

推荐答案

讽刺的是,你已经在全球范围内保存了 projID (更好地说, 中的窗口对象在声明中使用 var

Well, ironically you're already saving projID globally (better said, within the window object by not using var in your declaration.

因此,一旦初始化,您就可以从代码中的任何位置访问 projID

So you already can access projID from everywhere in your code, once it's initialized.

无论如何这不是一个好的实践,相反,这是一个非常糟糕的实践。

Anyways this is not a good pratice, opposite, it's a very bad pratice.

我能想到的最好方法是创建您自己的应用程序对象,您可以在其中存储所有数据并从那里访问它。

The best way I can think of is to create you own application object where you store all your data and access it from there.

var your_application = {
    var foo  = 'bar',
        base = 'ball';        
};

// your_application.foo = 'another bar';

使用闭包可以推动这一原则函数式编程。存储和保持数据本地而非全局(窗口)的更好解决方案可能看起来像

You can push this principle pretty far with using closures and functional programming. An even better solution to store and keep data local and not global (window) could look like

var your_application = function(){
    var foo  = 'bar',
        base = 'ball';  

    return {
        getfoo: function(){
            return foo;
        },
        setfoo: function(newfoo){
            foo = newfoo;
        }
    };
});

var app = your_application();
app.setfoo('foobar!');
alert(app.getfoo());

这是有条不紊的 javascript编程的想法。那里有一些好书,我建议开始使用 Javascript:道格拉斯克罗克福德的好零件

That is the idea of methodical javascript programming. There a some good books out there, I would recommend to start of with Javascript: the good parts by Douglas Crockford.

另一个解决方案应该是jQuery .data()方法,该方法能够以全局哈希值存储数据。

Another solution for this should be the jQuery .data() method, which is capable of storing data in a global hash.

// store
$.data(document.body, 'projID', projID);

// access
var foo = $.data(document.body, 'projID');

// remove
$.removeData(document.body, 'projID');

这篇关于jQuery - 在函数之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆