当数组为空时锁定按钮

时间:2013-01-21 16:20:08

标签: javascript web

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

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

<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>

0 个答案:

没有答案