【问题标题】:jQuery namespace variable undeclaredjQuery 命名空间变量未声明
【发布时间】:2011-06-01 17:36:28
【问题描述】:

我有以下 JavaScript 代码:

function LoadWord(currentLanguage, currentId, isBadWord)
{
    var filePath, badFilePath;
    switch(currentLanguage) {
        case 'spanish':
                filePath = 'data/spanish.xml';
                badFilePath = 'data/badSpanish.xml';
            break;
        case 'catalan':
                filePath = 'data/catalan.xml';
                badFilePath = 'data/badCatalan.xml';
            break;

        default: return;
    }

    $.ajax({
        type: "GET",
        url: filePath,
        dataType: "xml",
        success: function(xml) {
            $(xml).find('word').each(function(){
                if($(this).find('id').text() == $.myNameSpace.cImgId)
                {
                    $.myNameSpace.currentWord = $(this).attr('name');
                    $.ajax({
                        type: "GET",
                        url: badFilePath,
                        dataType: "xml",
                        success: function(xml) {
                            $(xml).find('word').each(function(){
                                for(index = 0; index < 3; index++)
                                {
                                    if($(this).find('id').text() == $.myNameSpace.badWordsIds[index])
                                    {
                                        $.myNameSpace.badWords[index] = $(this).attr('name');
                                    }
                                }
                                UpdateWords();
                            });
                        },
                        error: function(x) { alert(x.responceText); }
                    });
                }
            });
        },
        error: function(x) { alert(x.responceText); }
    });
}

function UpdateWords()
{
    var languageWordPos = randomFromTo(1, 4);
    var index = 1;
    var badWordsIndex = 0;

    while (index < 5) {
        if (index == languageWordPos ) {
            SetTextToButton(index, $.myNameSpace.currentWord);
        }
        else {
            SetTextToButton(index, $.myNameSpace.badWords[badWordsIndex]);
            badWordsIndex++;
        }
        index++;
    }

    LoadUserOwnWord($.myNameSpace.lang, $.myNameSpace.cImgId);
}

function SetTextToButton(index, text)
{
    console.log("SetTextToButton");
    switch(index)
    {
        case 1:
            $('#Word1').html(text);
        break;
        case 2:
            $('#Word2').html(text);
        break;
        case 3:
            $('#Word3').html(text);
        break;
        case 4:
            $('#Word4').html(text);
        break;
        default: return;
    }
}

我在这一行声明$.myNameSpace.badWords

$.myNameSpace.badWords[index] = $(this).attr('name');

此行在第二个 ajax 调用成功函数中声明。

当我在这里尝试从$.myNameSpace.badWords 获取值时:

SetTextToButton(index, $.myNameSpace.badWords[badWordsIndex]);

我得到一个未声明的变量。此行位于UpdateWords() 函数内。

我正在使用 FireBug 进行调试,但在 $.myNameSpace 变量上看不到 BadWords

我该如何解决这个问题?

【问题讨论】:

  • index &lt; 3; index &lt; 5 你的循环在UpdateWords 中更大
  • @jen:SetTextToButton 现在是我的问题。
  • @Raynos:这不是错误。

标签: jquery undeclared-identifier


【解决方案1】:

您在 ajax 调用中声明了变量,因此它是该函数的本地变量,您只需在 ajax 调用之外实例化该变量,以便其他函数可以使用它。

【讨论】:

    【解决方案2】:

    正确的答案是我不能使用未初始化的数组。

    我可以在成功函数中做到这一点:

    $.myNameSpace.badWords = new Array(3);
    

    它可用于任何其他功能。

    【讨论】:

      猜你喜欢
      • 2016-04-30
      • 2012-07-19
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多