Javascript-帮助在2个数组/堆栈之间移动
我有两个数组和一个变量。我可以将一个项目推入变量,然后在第二次单击时,将其添加到数组中。但是,我有另一个按钮,可以将项目从该数组移回变量,然后将变量移到另一个数组中。 我的代码在第一部分工作,但是我的函数现在在第二部分加载,有什么帮助吗 这一部分似乎不起作用Javascript-帮助在2个数组/堆栈之间移动,javascript,html,arrays,list,stack,Javascript,Html,Arrays,List,Stack,我有两个数组和一个变量。我可以将一个项目推入变量,然后在第二次单击时,将其添加到数组中。但是,我有另一个按钮,可以将项目从该数组移回变量,然后将变量移到另一个数组中。 我的代码在第一部分工作,但是我的函数现在在第二部分加载,有什么帮助吗 这一部分似乎不起作用 <INPUT type=button value="Back" onClick='txtPop.value = popBackStack();showStack(theList);pushForStack(curUrl); showS
<INPUT type=button value="Back" onClick='txtPop.value = popBackStack();showStack(theList);pushForStack(curUrl);
showStack2(theList2);'>
整个代码如下:
<SCRIPT>
var backStack = new Array();
var forStack = new Array();
var curUrl = document.getElementById("txtPop");
function pushStack(newVal) {
backStack.push(curUrl);
curUrl = newVal;
}
function pushForStack(newVal) {
CurUrl = newVal;
forstack.push(curUrl);
}
function popBackStack() {
var popVal = backStack.pop();
if (popVal == undefined) return "Nothing left!";
else return popVal
}
function popForStack() {
var popVal = forStack.pop();
if (popVal == undefined) return "Enter a new URL";
else return popVal;
}
function showStack(theSelect) {
theSelect.options.length = 0;
for (var i = 0; i < backStack.length; i++) {
var theOption = new Option(backStack[i]);
theSelect.options[theSelect.options.length] = theOption;
}
}
function showStack2(theSelect) {
theSelect.options.length = 0;
for (var i = 0; i < forStack.length; i++) {
var theOption = new Option(forStack[i]);
theSelect.options[theSelect.options.length] = theOption;
}
}
</SCRIPT>
<table width="104%" height="364" border="5" cellpadding="3" cellspacing="3">
<tr>
<th width="30%" height="78" scope="col"><p>
<INPUT type=button value="Back" onClick='txtPop.value = popBackStack();showStack(theList);pushForStack(curUrl);
showStack2(theList2);'></p></th>
<th width="46%" scope="col"><p>
<center>
<INPUT type=text name=txtPush>
<INPUT type=button value="Push" onClick='pushStack(txtPush.value);txtPush.value="";txtPop.value = curUrl; showStack(theList);'>
</center>
</p></th>
<th width="24%" scope="col"><p><INPUT type=button value="Forward" onClick="txtPop.value = popBackStack();showStack2(theList2);"></p></th>
</tr>
<tr>
<td><p><center>
<SELECT name="theList" size=12>
</SELECT>
</center></p></td>
<td><p><center><INPUT type=textki name=txtPop size=25></center></p></td>
<td><center>
<SELECT name="theList2" size=12>
</SELECT>
</center></td>
</tr>
</table>
var backStack=新数组();
var forStack=新数组();
var curUrl=document.getElementById(“txtPop”);
函数pushStack(newVal){
backback.push(curUrl);
curUrl=newVal;
}
函数pushForStack(newVal){
CurUrl=newVal;
forstack.push(curUrl);
}
函数popBackStack(){
var popVal=backStack.pop();
如果(popVal==未定义),则返回“不剩任何内容!”;
否则返回popVal
}
函数popForStack(){
var popVal=forStack.pop();
如果(popVal==未定义)返回“输入新URL”;
否则返回popVal;
}
函数显示堆栈(选择){
select.options.length=0;
对于(变量i=0;i
您的代码中有许多问题
document.getElementById('txtPop')
不应该返回任何内容,因为您没有具有该ID的元素。您有一个具有该名称的元素,但没有IDcurUrl
推入堆栈,但curUrl
(正确初始化时)是一个元素,而不是一个值。。。我不相信你想这么做哎呀!这需要在HTML属性中加入大量javascript。如果您将代码放入javascript函数,然后在单击事件中调用该函数,您会发现生活会轻松得多。。。或者通过使用该html属性,或者通过在文档onLoad代码中显式添加处理程序。