在html页面中使用not defined
事件调用嵌套函数时,在控制台上获取onClick
错误。我尝试在其他帖子上搜索答案,但未能。我认为这是一个范围或关闭问题。但不太确定如何解决它。
我有html拉入外部.js文件,并具有以下功能。我在mySelection1
函数中调用主函数checkMySelection
。并在mySelection1
事件上的主要html上调用onClick
中的嵌套函数。目标是根据它们单击的按钮更改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>
看起来嵌套函数完全被忽略了......:/我错过了什么?还有更好的方法吗?
**** UPDATE BELOW ****大家好,看看现代模式解决原始帖子...我开始使用以下代替函数方法中的嵌套函数...
//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索引值必须根据它们来自哪个'卡'而改变(本文评论中的上下文)...我需要另一个具有相同功能的对象才能更改'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>
您的代码中有很多未声明的内容,因此调试它有点困难。但是你的语法有一些问题,这是代码。
//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);