在多功能中使用变量 [英] Using variables in multiple function

查看:101
本文介绍了在多功能中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中:

我想在上一个(第二个)函数中使用第二个函数中定义的"id".我以为使用"var"一词就足够了,但事实并非如此.

I would like to use the "id" which is defined in the second function in the last (second) function. I thought using the "var" term would suffice but it doesn't.

有人知道我该怎么做吗?

Does anyone know how i could do it?

jQuery("td.setting").live("click",function(){
            text = jQuery(this).text();
            var id = jQuery(this).attr("id");

            jQuery(this).replaceWith("<td class=\"update\"><input class='inputSetting' type='text' value="+text+">  <img class=\"accept\" alt=\"accept button\" src='images/accept.png'></td>");
            console.log(id);
    });
    jQuery("img.accept").live("click",function(){

            jQuery("td.update").replaceWith("<td class=\"setting\"><proc name='C_Settings::getSavedSetting' params='"+id+"'></proc></td>");
    });

推荐答案

如果使用var,则该变量将成为该函数的局部变量.您可能想在两个函数之外定义"id",由于闭包,两个函数都可以使用它.

If you use var, the variable becomes local to that function. You may want to define 'id' outside of both functions and it will be available to both the functions due to closures.

var id = null;
jQuery("td.setting").live("click",function(){
                        text = jQuery(this).text();
                        id = jQuery(this).attr("id");

                        jQuery(this).replaceWith("<td class=\"update\"><input class='inputSetting' type='text' value="+text+">  <img class=\"accept\" alt=\"accept button\" src='images/accept.png'></td>");
                        console.log(id);
        });
        jQuery("img.accept").live("click",function(){

                        jQuery("td.update").replaceWith("<td class=\"setting\"><proc name='C_Settings::getSavedSetting' params='"+id+"'></proc></td>");
    });

如果您向我们展示您的HTML并告诉我们您要做什么,我们可能可以找到避免使用此类ID的方法. [提示:看一下jQuery中可用的数据方法.]

If you show us your HTML and tell us what you are trying to do, we can probably find a way to avoid using id like this. [Hint: Look at the data method available in jQuery.]

这篇关于在多功能中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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