选择器的值在循环

时间:2015-10-10 20:37:40

标签: javascript jquery

<table class="list" border="0" cellspacing="0" cellpadding="0">
  <tr class="dataRow">
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">1</a>
    </td>
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">2</a>
    </td>
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">3</a>
    </td>    
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">Homepage</a>
    </td>
  </tr>
  <tr class="dataRow">
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">1</a>
    </td>
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">2</a>
    </td>
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">3</a>
    </td>
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">Blog</a>
    </td>
  </tr>
  <tr class="dataRow">
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">1</a>
    </td>
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">2</a>
    </td>
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">3</a>
    </td>  
    <td scope="row" class="dataCell">
      <a href="https://test.com/test" class="ng-binding">Contact</a>
    </td>
  </tr>
</table>


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

for(var i=0; i<tableRow.length; i++){

  console.log(links[i]);
}

不明白为什么links的值为undefined 我已经尝试在循环中创建链接变量但是我得到了相同的结果 - 我认为它因为链接不是数组?如果是这样的话,我该怎么做?

我添加了标记以更好地解释我的问题...我正在尝试从表格单元格中获取链接并对其进行.click()

1 个答案:

答案 0 :(得分:-3)

这是jQuery解决方案,但如果这是一个有角度的应用程序,你应该使用指令

  

var links = $(&#39; .dataCell:nth-​​child(4).ng-binding&#39;);

此对象长度为0.了解nth-child如何工作https://api.jquery.com/nth-child-selector/

我想你想点击每个单元格中的最后一个链接。

var dataRow = $('.dataRow');
dataRow.each(function(ind, elem){
    console.log($(this).find('.ng-binding').eq(3));
    $(this).find('.ng-binding').eq(3).click();
});