为什么在再次调用函数时未定义变量

时间:2015-10-11 15:44:25

标签: javascript jquery

看起来像一个范围问题我无法理解为什么在调用函数两次时变量links未定义

<table class="list" border="1" cellspacing="0" cellpadding="0">
  <tbody>
    <tr class="headerRow">
      <th scope="col" class="numericalColumn zen-deemphasize">Display Order</th>
      <th scope="col" class=" zen-deemphasize">Message</th>
      <th scope="col" class=" zen-deemphasize">Description</th>
      <th scope="col" class=" zen-deemphasize">Media File Name</th>
      <th scope="col" class="zen-deemphasize ">Location</th>
      <th scope="col" class="zen-deemphasize ">Status</th>
      <th scope="col" class=" zen-deemphasize">data</th>
    </tr>


    <tr class="dataRow">
      <th scope="row" class="dataCell">
        1
      </th>
      <td scope="row" class=" dataCell">
        <a href="#">Homepage</a>
        <div class="mouseOverInfoOuter" tabindex="0">

        </div>
      </td>
      <td class="dataCell">
        Homepage
      </td>
      <td class="dataCell">
        <a href="http://test.com/test/zip1" class="zipUrl">
          test1.zip
        </a>
      </td>
      <td scope="row" class="dataCell">
        test1.zip
      </td>
      <td scope="row" class="dataCell">
        Content Server
      </td>
      <td scope="row" class="dataCell">
        Available for download
      </td>
      <td scope="row" class="dataCell">
        File not available.
      </td>
      <td scope="row" class="dataCell">

      </td>
      <td class=" dataCell ">
        <a href="#"></a>
      </td>
    </tr>

    <tr class="dataRow">
      <th scope="row" class="dataCell">
        2
      </th>
      <td scope="row" class=" dataCell">
        <a href="#">contact</a>
        <div class="mouseOverInfoOuter" tabindex="0">  </div>
      </td>
      <td class="dataCell">
        contact
      </td>
      <td class="dataCell">
        <a href="http://test.com/test/zip2" class="zipUrl">
          test2.zip
        </a>
      </td>
      <td scope="row" class="dataCell">
        test2.zip
      </td>
      <td scope="row" class="dataCell">
        Content Server
      </td>
      <td scope="row" class="dataCell">
        Available for download
      </td>
      <td scope="row" class="dataCell">
        File not available.
      </td>
      <td scope="row" class="dataCell">

      </td>
      <td class=" dataCell ">
        <a href="#"></a>
      </td>
    </tr>

    <tr class="dataRow">
      <th scope="row" class="dataCell">
        2
      </th>
      <td scope="row" class=" dataCell">
        <a href="#"> blog</a>
        <div class="mouseOverInfoOuter" tabindex="0"></div>
      </td>
      <td class="dataCell">
        blog
      </td>
      <td class="dataCell">
        <a href="http://test.com/test/zip3" class="zipUrl">
          test3.zip
        </a>
      </td>
      <td scope="row" class="dataCell">
        test3.zip
      </td>
      <td scope="row" class="dataCell">
        Content Server
      </td>
      <td scope="row" class="dataCell">
        Available for download
      </td>
      <td scope="row" class="dataCell">
        File not available.
      </td>
      <td scope="row" class="dataCell"></td>
      <td class=" dataCell ">
        <a href="#"></a>
      </td>
    </tr>

  </tbody>
</table>


<script>

    (function init() {

        var tableRow = document.querySelectorAll('.dataRow');
        var links = $('.dataCell:nth-child(4) .zipUrl');

        if (tableRow.length === 0) return;

        var index = 0;

        function click() {

            if (index < tableRow.length) {
              //comment out line 14 to see console.log working normally
              $(links[index++]).click();
              console.log('Clicked on... ' + links[index++])
              click();

            }

        }
        click();

      }());

</script>

实施例

http://jsfiddle.net/Grundizer/6p60bkg9/

我正在尝试点击表格Media File Name列中的每个链接,我无权访问添加或删除css类的源代码,我必须使用标记由服务器生成。

1 个答案:

答案 0 :(得分:0)

主要问题是,index$(links[index++]).click();增加了两倍,另一个在{x}}增加了一个空数组元素。

执行此操作 - &gt;

console.log('Clicked on... ' + links[index++]);
顺便提一下,有一些语法错误,我在解决方案代码中修复了它。