【问题标题】:Calling nested Function onClick event not recognizing nested functions调用嵌套函数 onClick 事件不识别嵌套函数
【发布时间】:2019-02-14 21:47:19
【问题描述】:

在 html 页面中使用 onClick 事件调用嵌套函数时,控制台上出现 not defined 错误。我尝试在其他帖子上搜索答案,但无法。我认为这是一个范围或关闭问题。但不太确定如何解决。

我有 html 拉入一个外部 .js 文件,并具有以下功能。 我在checkMySelection 函数中调用主函数mySelection1。并在 onClick 事件的主 html 上调用 mySelection1 内的嵌套函数。目标是根据他们单击的按钮更改 currentPage 变量(它最初在 .js 文件的开头声明为全局变量。)

//first function
 function mySelection1() { 
  alert("in mySelection1 function");
  var SubMenu1 = function() {
    currentPage = 1;
    alert("current XML node is " + currentPage);
  };
  var SubMenu2 = function() {
    currentPage = 2;
    alert("current XML node is " + currentPage);   
  };
  var SubMenu3 = function() {
    currentPage = 3;
    alert("current XML node is " + currentPage);   
  };
}
//second function
function checkMySelection() {

  SearchString = window.location.search.substring(1);
  VariableArray = SearchString.split('=');
  mySelection = VariableArray[1];
  console.log(mySelection); 

  switch (mySelection) {
    case '1':
      alert("Case 1");
      mySelection1();
    break;
    case '2':
      //alert("Case 2");
      //mySelection2();
    break;
    default:
      alert("Something went wrong...");
  }
}

<! -- CODE IN HTML BELOW -->

<button id="defaultItem" class="menuItem active" onclick="openItem(event, 'item1'); SubMenu1()">Item 1</button>
          <button class="menuItem" onclick="openItem(event, 'item2'); SubMenu2()">Item 2</button>
          <button class="menuItem" onclick="openItem(event, 'item3'); SubMenu3()">Item 3</button>

看起来嵌套函数被完全忽略了......:/我错过了什么?还有更好的方法吗?

****下面更新**** 大家好,正在寻找现代模式以解决原始帖子...我从以下内容开始,而不是函数方法中的嵌套函数...

//Using Object Literal Notation
var mySelection1 = {
  mySubMenu1: function() {
    currentPage = 1;
    alert("current XML node is " + currentPage);
  },
  mySubMenu2: function() {
    currentPage = 2;
    alert("current XML node is " + currentPage);   
  },
  mySubMenu3: function() {
    currentPage = 3;
    alert("current XML node is " + currentPage);   
  }
}

我知道调用我必须使用的函数 mySelection1.moduleSubMenu1(); mySelection1.moduleSubMenu2 ();和 mySelection1.moduleSubMenu3();我将这些添加到我的 html 页面按钮中......因为我只希望它们在按钮 onClick 事件中被调用。

<button id="defaultItem" class="menuItem active" onclick="openItem(event, 'video'); mySelection1.moduleSubMenu1()">Video</button>
<button class="menuItem" onclick="openItem(event, 'resources'); mySelection1.moduleSubMenu2()">Resources</button>
<button class="menuItem" onclick="openItem(event, 'quick-check'); mySelection1.moduleSubMenu3()">Quick Check</button>

如果我只使用这个对象,这非常有效......但是,它让我想到了我想要完成的问题(为什么我之前尝试使用嵌套函数)......我怎样才能做到这一点var 对象可重用? 'currentPage' var 索引值必须根据它们来自的“卡”而改变(这篇文章的 cmets 中的上下文)...我需要另一个具有相同功能的对象才能更改“currentPage”。 ..即

    //Second object needed
    var mySelection2 = {
      mySubMenu1: function() {
        currentPage = 6;
        alert("current XML node is " + currentPage);
      },
      mySubMenu2: function() {
        currentPage = 7;
        alert("current XML node is " + currentPage);   
      },
      mySubMenu3: function() {
        currentPage = 8;
        alert("current XML node is " + currentPage);   
      }
    }

这很好,但问题是我想使用相同的子菜单项,并且它们在onClick上已经有了之前的object.function...

<button id="defaultItem" class="menuItem active" onclick="openItem(event, 'video'); mySelection1.moduleSubMenu1()">Video</button>
<button class="menuItem" onclick="openItem(event, 'resources'); mySelection1.moduleSubMenu2()">Resources</button>
<button class="menuItem" onclick="openItem(event, 'quick-check'); mySelection1.moduleSubMenu3()">Quick Check</button>

【问题讨论】:

  • 当您在另一个函数中定义函数SubMenu1 时,它们只存在于封闭函数的范围内。它们不存在于外部。不清楚为什么它们首先是嵌套的。
  • 但是嵌套函数(SubMenu1 等)在mySelection1 之外是不可见的。您可能需要module pattern 之类的东西,即显示模块模式来公开它们。即使那样,我也不确定这里的目标是什么。将函数嵌套到其他函数中的整个方法似乎是错误的。
  • @MarkMeyer 谢谢,这证实了我对它是范围问题的怀疑。这是我想要完成的:主页上有多个“卡片”,用户可以单击以获取更多信息。根据他们的选择,我正在更改“mySelection”变量的值并将其传递给他们选择的新打开的内容页面。 'checkMySelection' 函数读取值并运行 switch 并调用函数(mySelection1--在示例代码中提供,并将具有相应的其他函数,即 mySelection2、mySelection3),其中将包含相同的子菜单嵌套函数。
  • @MarkMeyer(续) 然后在 html 按钮的 onClick 事件上调用它们。我在 .js 文件的开头声明了全局“var currentPage”……嵌套的子菜单函数会将“currentPage”的值更改为相应的 XML 节点索引,该索引将动态填充 xml 内容。但由于它是一个范围问题,一旦它到达嵌套函数,它就无法工作。
  • @VLAZ 谢谢!我会研究它并试一试:)...希望上面我的 cmets 中的上下文能更好地描绘我的逻辑。

标签: javascript html


【解决方案1】:

您的代码中有很多未声明的内容,因此调试起来有点困难。但是,您的语法存在一些问题,这是代码。

         //first function
     function mySelection1() {
         alert("in mySelection1 function");

         function SubMenu1() {
             currentPage = 1;
             alert("current XML node is " + currentPage);
         }

         function SubMenu2() {
             currentPage = 2;
             alert("current XML node is " + currentPage);
         }

         function SubMenu3() {
             currentPage = 3;
             alert("current XML node is " + currentPage);
         }
     }
     //second function
     function checkMySelection() {

         SearchString = window.location.search.substring(1);
         VariableArray = SearchString.split('=');
         mySelection = VariableArray[1];
         console.log(mySelection);

         switch (mySelection) {
             case '1':
                 alert("Case 1");
                 mySelection1();
                 break;
             case '2':
                 //alert("Case 2");
                 //mySelection2();
                 break;
             default:
                 alert("Something went wrong...");
         }
     }
     var button1 = document.getElementById("defaultItem");
     button1.addEventListener("click", SubMenu1);

【讨论】:

  • 谢谢,'currentPage'、'mySelection'、'SearchString' 和 'VariableArray' 是在我的 .js 文件开头声明的全局变量。这些都相应地工作,但嵌套函数被忽略。从我在之前的 cmets 中看到的情况来看,这似乎是一个范围问题。 (我提供的按钮元素的 html 示例代码位于我的 html 文档中)