我尝试从一堆<a> tags: Explain where I&#39;m going wrong

时间:2015-05-18 18:02:50

标签: javascript jquery algorithm

From a tbody element

<tbody id="results">
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-roman_column.png">/../../Assets/Microsoft-Azure-roman_column.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>roman_column.png</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-runphp.cmd">/../../Assets/Microsoft-Azure-runphp.cmd</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>runphp.cmd</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-runphp.cmd">/../../Assets/Microsoft-Azure-runphp.cmd</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>runphp.cmd</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-Picture1.png">/../../Assets/Microsoft-Azure-Picture1.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>Picture1.png</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png">/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>vertical-align-scrnsht.png</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png">/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>vertical-align-scrnsht.png</td>
   </tr>
</tbody>

my procedure attempts to grab the hrefs and stick them in an array called links:

        var resultRows = $('#results > tr > td > a');
        for (var thisAnchor in resultRows) links.push($(this).attr('href'));
        for (var thisLink in links) console.log(thisLink); // test

But that test is logging

0
1
.
.
.
171
172

to the console rather than the expected

/../../Assets/Microsoft-Azure-roman_column.png
/../../Assets/Microsoft-Azure-runphp.cmd
/../../Assets/Microsoft-Azure-runphp.cmd
/../../Assets/Microsoft-Azure-Picture1.png
/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png
/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png

Why is that and how do I fix it?

3 个答案:

答案 0 :(得分:2)

你可以使用jQuery .each,就像这样:

p = mdb.models[name_model].parts[name_part_1]
e = p.edges
pickedEdges = e.getByBoundingBox(((cos(alpha_rad)*ri)-delta_p),((sin(alpha_rad)*ri)-delta_p),0.0,
                                 ((cos(alpha_rad)*d_core/2)+delta_p),((sin(alpha_rad)*d_core/2)+delta_p),0.0)
p.seedEdgeByBias(biasMethod=SINGLE, end2Edges=pickedEdges, ratio=bias_f, number=elem_num_rad, constraint=FINER)

答案 1 :(得分:1)

首先,你的选择器没有任何元素,所以改为

$('#results [href]')

其次,使用.each代替for循环

$('#results [href]').each(function(){
    links.push($(this).attr('href'));
});

最后,改变

for (var thisLink in links) console.log(thisLink);

for (var thisLink in links) console.log(links[thisLink]);

当您在javascript中迭代foreach循环时

for(var key in collection)

key,在这种情况下是实际的键或索引,因为你有一个数组

看到它正常工作here

答案 2 :(得分:1)

首先,您的HTML无效。您有一个未公开的input标记。

接下来,您的tbody需要被table代码

包裹

然后,如果您要查找hrefs数组,则应使用jQquery.map函数。

这是一个工作示例: https://jsfiddle.net/p6h7o32m/