0

私はjqueryの初心者です。立ち往生しており、助けていただければ幸いです。現在、特定のページからデータを取得するためのツールを構築しています。ページは次のようになります。

<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
 <tbody>  //This is data group 1    
  <tr id="parent0">   //Record 1
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
  <tr id="parent0">   //Record 2
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
 </tbody>
</table>

<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
 <tbody>  //This is data group 2    
  <tr id="child0">   //Record 1
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
  <tr id="child0">   //Record 2
    <td align="left"> <---------- text1 here -------> </td>
    <td align="left"> <---------- text2 here -------> </td>
    <td align="left"> <---------- text3 here -------> </td>
  </tr>    
 </tbody>
</table>

以下は、jquery のスニペットです。

ancestor = $(this).closest("tr[id]");

  matchedElement = $(this).first();
  originalBgColor = $(matchedElement).css('background-color');
  $(matchedElement).css('background-color', 'green');
  $(matchedElement).bind('click.annotator', function(event) {
    event.stopPropagation();
    event.preventDefault();
    self.port.emit('show',
      [
        document.location.toString(),
        $(ancestor).attr("child0"),
        $(matchedElement).text()
      ]

<tr>IDがparent0とchild0のブロックだけからすべてのデータをキャプチャしようとしています。

現在の作業状態では、ツールは 2 つのテーブル内のすべてのデータをテキストとしてキャプチャします。<TR>理想的には、すべてのブロックを個別にキャプチャし、それらを配列に入れて、反復できる ようにしたいと考えています。

4

3 に答える 3

2

クラスを使用する必要がある場所で ID 要素を使用しているようです。

それ以外の

<tr id="child0">
<tr id="parent0">

これを行う

<tr class="child0">
<tr class="parent0">

IDとは異なり、 class 属性を使用して複数の要素に同じ値を割り当てることができます。

次に、このようにすべて選択できます

$("tr.child0,tr.parent0").each(
                          function() {
                             //this will be fired for each matched element
                             $(this).doSomething();
                          }
                         )
于 2012-01-11T01:08:41.130 に答える
0
$('tr[id="child0"]').each(function() {
                         //this will be fired for each matched element
                         $(this).doSomething();
                      }
                     );

このようにして、IDがchild0であるすべてのtrを取得します。詳細については、このリンクを確認してください http://api.jquery.com/category/selectors/

于 2012-01-11T13:27:48.793 に答える