<script type="text/javascript">
  arr = new Array("tr.pay0","tr.pay1");  function setDispaly1() {
    for (i=0; i<2; i++) {
      $(arr[i]).hide();  //这里没出错
    }
  }  function setDispaly2() {
    for (i=0; i<2; i++) {
      $(arr[i]).Show();  //可这里说没定义,出错
    }
  }
</script>见上例,为何同一个数组,在不同的地方使用,有的地方出错有的地方没出错?求数组在JavaScript的正确用法!

解决方案 »

  1.   

    这个不是你的数组用错了。是jquery报错了。应该是报的缺失对象,对吧。。建议你先看看jquery的api把$()这个里面放什么东西代表什么意思,楼主还有待了解噢
      

  2.   

    这里应该不是数组的错误 推荐使用火狐的firebug调试 他对js调试比较友好。
      

  3.   

    $(arr[i]).Show();
    要小写,show()
      

  4.   

    $(arr[i]).Show(); //可这里说没定义,出错
    $(arr[i]).show(); //换成小写,js严格区分大小写
      

  5.   

    喂~、诸位可说的够严厉的。其实,$(arr[i]).Show() 那只是俺举的例子,而且是录入问题,
    在实际程序里没这段!在实际程序里都是$(arr[i]).hide();
    第一个没错,第二个就出错,对象没定义!
      

  6.   

    还是没有理解jquery。<input type="submit" id="hideDiv" value="hide">
    <input type="submit" id="showDiv" value="show">
    <div id="div1">div11</div>
    <div id="div2">div12</div>
    <script type="text/javascript">
        var arr = ['div1', 'div2'];
        $('#showDiv').click(function(){
            $(arr).each(function(){
                $('#' + this).show();
            });
        });    $('#hideDiv').click(function(){
            $(arr).each(function(){
                $('#' + this).hide();
            });
        });
    </script>
      

  7.   


    <table>
        <tr class="pay0">
            <td>1111</td>
        </tr>
        <tr class="pay1">
            <td>1111</td>
        </tr>
    </table>
    <script type="text/javascript">
        var arr = ["pay0","pay1"];
        $('#showDiv').click(function(){
            $(arr).each(function(){
                $('tr.' + this).show();
            });
        });    $('#hideDiv').click(function(){
            $(arr).each(function(){
                $('tr.' + this).hide();
            });
        });
    </script>
      

  8.   

    那你不举$(arr[i]).Show(); //可这里说没定义,出错
     这个例子不就没问题了。没有Show这个方法肯定报未定义了