我有2个数组和一个变量。我能够将项目推送到变量中,然后在第二次单击时将其添加到数组中。但是,我有另一个按钮,用于将项目从该Array移动,返回到Variable并将变量移动到另一个Array中。 我的代码在第一部分工作,但我的功能现在加载第二部分,请帮忙吗?
这部分似乎不起作用
<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>
答案 0 :(得分:0)
您的代码中存在许多问题
document.getElementById('txtPop')
应该不返回任何内容,因为您没有带有该ID的元素。您有一个名称的元素,但不是 id 。curUrl
推入堆栈,但curUrl
(正确初始化时)是一个元素,而不是一个值......我不相信&# 39,你正在尝试做什么。