<TABLE cellSpacing="0" cellPadding="0" width="99%" border="0" id="BaseTabel6">
    <TR>
<TD width="28%">Company name of chinese:</TD>
<TD width="72%" height="25">
<span id="Baselabel2"></span></TD>
    </TR>
    <TR>
          <TD bgColor="#cccccc"></TD>
<TD bgColor="#cccccc" height="1"></TD>
    </TR>
</TABLE>我想让当<span id="BaseLabe2"></span>为空字符时不显示表格我写的JS代码为:
with ( document.Form1 )
{
  for(int i=0;i<17;i++)
  {
     var t = document.getElementById('BaseLabel'+i);
     if(t.innerText=="")
     document.getElementById('BaseTabel'+i).style.display = "none";
  }
} 可没有实现我所要的结果,各位大哥,请解答解答,小弟在此谢过!

解决方案 »

  1.   

    js代码没有太大问题,看下HTML代码中相关元素的id有没有写错。
      

  2.   

    with ( document.Form1 )
    {
      for(int i=0;i<17;i++)
      {
         var t = document.getElementById('BaseLabel'+i);
         if(t.innerText=="")
         document.getElementById('BaseTabel'+i).style.display = "none";
      }
    }with里面不应该再用document对象,会引起找不到对象的错误,且Js里只有弱类型,没有int的定义方法
    应该是  for(var i=0;i<17;i++)
      {
         var t = document.getElementById('BaseLabel'+i);
         if(t.innerText=="")
         document.getElementById('BaseTabel'+i).style.display = "none";
      }
      

  3.   

    js写的有些问题,首先你的for循环里每次document.getElementById('BaseLabel'+i);得到的对象有可能是null,如果你没有判断是否为null就用它的t.innerText属性的化,会报错,其次那个页面也没看到form1表单,另外还有就是上面的人说的数据类型的问题,我是这么写的可以实现
    <script>
    function aaa()
    {
      for(var i=0;i<17;i++)
      {
         var t = document.getElementById('BaseLabel'+i);
     if(t != null){
     alert(t.innerText);
     if(t.innerText == '')
    document.getElementById('BaseTabel'+i).style.display = "none";  
    }
      }

    </script>
    <body onLoad="aaa()">
    <form name="form1">
    <TABLE cellSpacing="0" cellPadding="0" width="99%" border="0" id="BaseTabel6">
        <TR>
    <TD width="28%">Company name of chinese:</TD>
    <TD width="72%" height="25">
    <span id="Baselabel6"></span></TD>
    </TR>
        <TR>
    <TD bgColor="#cccccc"></TD>
    <TD bgColor="#cccccc" height="1"></TD>
        </TR>
    </TABLE>
    </form>
    </body>
    还有我就是不明白with(document.form1){}这个用法,希望能回答一下,我试了好几次也没成功
      

  4.   

    <td id=id1> id1.style.display="none" 
      

  5.   

    问题如下
    1 循环的时候 定义 int i 这里出问题了吧 应该时var i
    2 js里的BaseTabel  与 HTML里的 Baselabel  搞错了 吧 
    正确如下
    with (document.Form1)
        {
              for(var i=0;i<17;i++)
              {
                 var t = document.getElementById('Baselabel'+i); 
                 if (t)
                 {       
                         if(t.innertText=="")
                         {
                            document.getElementById('Baselabel'+i).style.display = "none";
                         }
                 } 
              }
        }