这样吧,下面的例子:
<DIV id="myid">
<table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>1</td> </tr></table>
<table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>2</td>  </tr></table>
<table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>3</td>  </tr></table>
<table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>4</td> </tr></table>
<table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>5</td>  </tr></table></DIV>
<input type=button value="展开所有table" onclick="show('show')">
<input type=button value="关闭所有table" onclick="show('hidden')">   请问,这个show函数怎么写?

解决方案 »

  1.   

    try like this:
    <script language="JavaScript">
    <!--
    function show(flag){
    var c=document.getElementById("myid").childNodes
    for(i=0;i<c.length;i++)c[i].style.display=flag
    }
    //-->
    </script>
    <DIV id="myid">
    <table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>1</td> </tr></table>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>2</td>  </tr></table>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>3</td>  </tr></table>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>4</td> </tr></table>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" >  <tr>    <td>5</td>  </tr></table>
    </DIV>
    <input type=button value="展开所有table" onclick="show('block')">
    <input type=button value="关闭所有table" onclick="show('none')">
      

  2.   

    <script language="javascript">function show(ok)
    {
    eval(document.all(ok)).style.display=(eval(document.all(ok)).style.display=="none")?"":"none";
    }function closeall()
    {
      for (i=1;i<=6;i++) {
        document.all("layer"+i).style.display="none"; 
      }
     
    }
    </script>
        <DIV id="layer1">层1</DIV>
        <DIV id="layer2">层2</DIV>
        <DIV id="layer3">层3</DIV>
        <DIV id="layer4">层4</DIV>
        <DIV id="layer5">层5</DIV>
        <DIV id="layer6">层6</DIV>
        <DIV id="ok" name="kkk">上面的...</DIV>         
     <br>
        <input type=button value="打开或关闭层3" onClick="show('layer3');">
        <input type=button value="关闭所有的层" onClick="closeall();">
        <input type=button value="打开层3并关闭其它所有的层" onClick="closeall();show('layer3')">
      

  3.   

    <html>
    <head>
    <script language="javascript">function show(ok) //????
    {
      document.all(ok).style.display=document.all(ok).style.display==""?"none":"";
    }function closeall()
    {
      for (i=1;i<7;i++) {
        document.all("layer"+i).style.display="none"; //???
      }
     
    }function myopen(ok2)
    {
      for (i=1;i<7;i++) {
        document.all("layer"+i).style.display="none"; //???
      }
      document.all(ok2).style.display=""; 
    }</script>
    </head><body>
        <DIV id="layer1">层1</DIV>
        <DIV id="layer2">层2</DIV>
        <DIV id="layer3">层3</DIV>
        <DIV id="layer4">层4</DIV>
        <DIV id="layer5">层5</DIV>
        <DIV id="layer6">层6</DIV>
        <DIV id="ok" name="kkk">上面的...</DIV>         
     <br>
        <input type=button value="打开或关闭层3" onClick="show('layer3');">
        <input type=button value="关闭所有的层" onClick="closeall();">
        <input type=button value="打开层3并关闭其它所有的层" onClick="myopen('layer3');">
    </body>
    </html>