从存在的问题与jQuery渲染HTML表格提取零件(Problems extracting part

2019-10-30 01:28发布

我需要从多个表中抢某些部分,将其保存,最后用一个jQuery模板生成一个新表。 但我有收集的第一个值的问题。

HTML

<table>
  <tr><td colspan="7"><a href="http://link/index.php?view=page&id=2961" target="_blank" title="title">atext1 atext2</a> - stuff 2 - <img src="img/icon_1.gif" class="icon" title="icon1" />12 - <img src="img/icon_2.gif" class="icon" title="icon2" />4 - <span title="long title"><img src="img/icon_3.gif" class="icon" /> stuff 5 </span></td></tr>
  <tr><th><span title="title1">th1</span></th><th title="title2">th2</th><th title="title3">th3</th><th title="title4">th4</th><th title="title5">th5</th><th title="title6">th6</th><th title="title7">th7</th></tr>
  <tr><td class="own" style="width:10px;">&nbsp;</td><td><a href="http://link/index.php?view=page2&id=1413" target="_blank" title="title">text</a></td><td><img src="img/icon_4.png" class="icon link" title="icon4" onclick="dothis(118965,[1324,1835,2296,2443,2960,2961,2962,2964,2976,3186,4901,4974]);" /> <a href="javascript:dothat('div1',118965);" title="div1">text</a></td><td><a href="http://link/index.php?view=page3&newid=188898" target="_blank" title="page3">text</a></td><td class="right">24 (9)</td><td class="right">827</td><td class="right">11</td></tr>
  MORE SIMILAR TRs HERE
</table>

JS

var savedData = [];
tbl = $('#table_1'); // Grab all content within div, which is a whole html table 
var data = [];
$('tr', tbl).each(function(i, tr) {
  // Begin the process to grab needed parts
  if (i == 0) { // first row is special. it is colspan'ed
    row = $('td', tr).html().split(' - ');  // Splitting seems like the only way to get smaller html parts to work with
    var a_href = $(row).find('a').attr('href'); // returns undefined
    var a_href = $(row[0]).find('a').attr('href'); // returns undefined
    var a_href = $('a', row).attr('href');  // returns undefined
    var a_href = $('a', row[0]).attr('href');  // returns undefined

    // Grab id only. I thought this would work
    data['id'] = $('a', row[0]).attr('href').match(/view=page&id=([0-9]+)/)[1];
    // Grab the other parts
  } else {
    // do stuff for other rows
  }
});
savedData.push[data];

1)为什么我会收到未定义? 分割内部HTML当“行”成为一个数组

2)I还具有至少1个TR只Th细胞。 难道更快那些从。每个(不含),像子选择,或者只是忽略它行(像if ($('td', tr).length > 0) { // do stuff } )?

3)如果我只是抢在所有TR然后使用jQuery.map所有TD中的所有文本()()大约要快3-4倍jQuery.each(I,V)。 任何想法,为什么?
我可以用$('td[colspan], tr)抢到第一排,但jQuery.each()是很容易理解,从而与合作

4)当我需要使用所收集的部分在模板,我可以然后使用savedData[0].id

Answer 1:

我有我的答案在这里如何从一个HTML字符串使用jQuery提取多个零件?



文章来源: Problems extracting parts from rendered HTML tables with jQuery