Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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 为什么这些全局变量在3个函数之间消失?_Javascript_Jquery - Fatal编程技术网

Javascript 为什么这些全局变量在3个函数之间消失?

Javascript 为什么这些全局变量在3个函数之间消失?,javascript,jquery,Javascript,Jquery,我有三个函数,在本例中,它们按以下顺序启动: function editmatch(){ defaultstyling(); $('.all_cons').css({display: 'none'}); $('.confirm_button'+id).css({display: 'inline'}); storecontents(); isediting = true; $(document).fin

我有三个函数,在本例中,它们按以下顺序启动:

function editmatch(){
        defaultstyling();
        $('.all_cons').css({display: 'none'});
        $('.confirm_button'+id).css({display: 'inline'});
        storecontents();
        isediting = true;
        $(document).find("td[id^='hero_column"+id+"']").html('<input id="select_hero" type="text" name="select_hero">');
        $(document).find("td[id^='result_column"+id+"']").html("<select name='winloss'><option value='Win'>Win</option><option value='Loss'>Loss</option></select>");
        $(document).find("td[id^='gamemode_column"+id+"']").html('<select name="gamemode"<option value="All Pick">All Pick</option><option value="Captains Mode">Captains Mode</option><option value="Captains Draft">Captains Draft</option></select>');
        $(document).find("td[id^='mmr_column"+id+"']").html('<input id="input_mmr" type="text" name="input_mmr">');
}
此函数用于在更改文本之前将文本存储在单元格内。请注意,这些变量是在我的编码页面顶部全局定义的。此警报显示的
herotemp
与以前的html“Rubick”相同

然后,一个按钮触发此代码(如有必要)以将内容更改回原始内容:

function abandonedit(){
       alert(herotemp);
       $(document).find("td[id^='hero_column"+id+"']").html(herotemp);
        $(document).find("td[id^='result_column"+id+"']").html(resulttemp);
        $(document).find("td[id^='gamemode_column"+id+"']").html(gametemp);
        $(document).find("td[id^='mmr_column"+id+"']").html(mmrtemp);
}

但是,上述函数中的警报显示
herotemp
变量(以及所有其他变量)为空。我认为一个全局声明的变量在所有函数中都是可用的?我是做错了什么还是误解了?

如果您在
var herotemp
中声明变量,您将有一个名为
herotemp
的局部(函数范围)变量,它阻止您访问全局变量
herotemp

这将使您的代码符合预期的行为

function storecontents(){   //HOLDS CONTENTS OF ROW
    /*var*/ herotemp = document.getElementById("hero_column"+id).innerHTML;
    var resulttemp = document.getElementById("result_column"+id).innerHTML;
    var gametemp = document.getElementById("gamemode_column"+id).innerHTML;
    var mmrtemp = document.getElementById("mmr_column"+id).innerHTML;
    alert(herotemp);
}
有时,您需要名为
x
的局部(函数范围)变量以及名为
x
的全局变量。为了实现这一点,您必须执行以下操作:

var x = 3;

function useX() {
    var x = 5;
    alert(x, window['x']);
}
或:

如果您在函数中使用var,这将仅在该函数中声明该变量。另外,如果您有一个声明为全局且名称相同的变量


要生成全局变量或使用全局变量,只需删除var

,但在其他3个变量之前可以吗pOf课程!!我明白你的意思。谢谢你的贡献。“这是一个新手犯的错误。”阿切尔,为什么不可以?好吧,幽默感在那里不起作用。您还需要从其他3个声明中删除
var
。哦,我不知道哪些变量需要全局变量,哪些不需要全局变量
herotemp
是文本中唯一提到的一个,所以这就是我所修复的。
var x = 3;

function useX() {
    var x = 5;
    alert(x, window['x']);
}
var x = 3;

function useX(x) {
    alert(x, window['x']);
}