1

我编写了一个模拟 Web 浏览器的脚本。用户在顶部的 URL 框中输入内容,当他们按下 go 时,它会显示在页面的中心。如果用户输入一个新的 url,当前的 url 存储在一个数组/堆栈中。当用户按下返回时,该项目从堆栈中弹出并显示在当前 url 中,当前 url 进入一个新 url 允许用户在网页中单击前进。

当 backstack 和 forStack 分别用尽时,我想使用 Javascript 将“禁用”属性设置到后退或前进按钮上。知道我会用什么代码来做到这一点吗?

<HTML>
<HEAD>
<TITLE>Stacking up!</TITLE>
<script>


var backStack = new Array(); 
var forStack = new Array();
var curUrl = "";

function pushStack(newVal) {
if (curUrl == "") 
curUrl = newVal;
else
   backStack.push(curUrl);
   curUrl = newVal;
}

function pushForStack(newVal) {
forStack.push(curUrl);
    curUrl = newVal;
}

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>



</HEAD>
<BODY>
<FORM>
<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(); pushForStack(txtPop.value); showStack(theList); showStack2(theList2);'></p></th>
    <th width="46%" scope="col"><p>
  <center>
  <INPUT type=text name="txtPush">
  <INPUT type=button value="Push" onClick='forStack.length=0; showStack2(theList2);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 = popForStack(); pushStack(txtPop.value); showStack(theList); 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" id="txtPop" size=25></center></p></td>
    <td><center>
    <SELECT name="theList2" size=12>
    </SELECT>
    </center></td>
  </tr>
</table>
</FORM>
<p>&nbsp;</p>
</BODY>
</HTML>
4

0 回答 0