2012-02-10 12 views
0

テーブル内の見出しを取得し、最初のimgタグの上に移動するJqueryコードがあります。それは動作しますが、個々の行の見出しの代わりに、テーブルのすべての単一見出しを取得するようです。前jqueryのみを使用して要素の見出しを移動する

<table cellspacing="0" border="1" style="border-collapse: collapse;" id="gvResults" class="daysouttable"> 
    <tr> 
     <td> 
      <div class="item clearfix"> 
       <img src="http://images.daysoutguide.co.uk/mini_6031-AttractionImage.jpg" class="first_image" /> 
       <h3 class="table_heading"> 
        Tate Modern</h3>         
      </div> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <div class="item clearfix"> 
      <img src="http://images.daysoutguide.co.uk/mini_6851-AttractionImage.jpg" class="first_image" /> 
       <h3 class="table_heading"> 
        Blood Brothers</h3>          
      </div> 
     </td> 
    </tr> 
</table> 

のjQuery:

$("div.item").each(function (index) { 
    $('.table_heading').insertBefore("table#gvResults img:first-child"); 
}) 

結果:

<table cellspacing="0" border="1" style="border-collapse: collapse;" id="gvResults" class="daysouttable"> 
    <tr> 
     <td> 
      <div class="item clearfix"> 
       <h3 class="table_heading"> 
        Tate Modern</h3> 
       <h3 class="table_heading"> 
        Blood Brothers</h3>     
       <img src="http://images.daysoutguide.co.uk/mini_6031-AttractionImage.jpg" class="first_image" /> 
      </div> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <div class="item clearfix"> 
       <h3 class="table_heading"> 
        Tate Modern</h3> 
       <h3 class="table_heading"> 
        Blood Brothers</h3> 
       <img src="http://images.daysoutguide.co.uk/mini_6851-AttractionImage.jpg" class="first_image" /> 
      </div> 
     </td> 
    </tr> 
</table> 

しかし、私が見えるようにHTMLマークアップしたい:

<table cellspacing="0" border="1" style="border-collapse: collapse;" id="gvResults" class="daysouttable"> 
    <tr> 
     <td> 
      <div class="item clearfix"> 
       <h3 class="table_heading"> 
        Tate Modern</h3>     
       <img src="http://images.daysoutguide.co.uk/mini_6031-AttractionImage.jpg" class="first_image" /> 
      </div> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <div class="item clearfix">     
       <h3 class="table_heading"> 
        Blood Brothers</h3>     
       <img src="http://images.daysoutguide.co.uk/mini_6851-AttractionImage.jpg" class="first_image" /> 
      </div> 
     </td> 
    </tr> 
</table> 

感謝を!

答えて

0

私はこれがあなたが探しているものを達成すると信じています。これにより、見出しはitemコンテナの最初の要素になります。

$(document).ready(function(){ 
    $(".table_heading", "#gvResults").each(function(){ 
     var $heading = $(this); 
     $heading.prependTo($heading.parent()); 
    }); 
}); 
+0

返信いただきありがとうございます。基本的にはそれを試しましたが、各行の見出しとして「Tate Modern」を置いているようです。私が必要とするのは、最初の行が "Tate Modern"のみを表示し、2番目の行が "Blood Brothers"のみを表示することです。データが外部データソースから取り込まれているため、htmlを直接編集できません。 –

+0

あなたの元のマークアップに基づいて私の応答を編集 –

+0

おかげで魅力的なように働いてくれてありがとう!感謝します! –

関連する問題