日期:2014-05-16  浏览次数:20351 次

求数组在JavaScript的正确用法
<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的正确用法!

------解决方案--------------------
这个不是你的数组用错了。是jquery报错了。

应该是报的缺失对象,对吧。。

建议你先看看jquery的api把。。。

$()这个里面放什么东西代表什么意思,楼主还有待了解噢。。。
------解决方案--------------------
这里应该不是数组的错误 推荐使用火狐的firebug调试 他对js调试比较友好。
------解决方案--------------------
还是没有理解jquery。
HTML code

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

------解决方案--------------------
HTML code

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