使用jquery将表格单元格中的'a'元素替换为另一个元素

时间:2013-02-23 12:37:28

标签: jquery

我有这张桌子:

<table border="0" class="tab_cadrehov">
       <tbody>
           <tr class="tab_bg_1">
               <td valign="top"><a id="User_245_245" href="/dailyTickets/front/contacts.form.php?id=245">Gov0N</a></td>
               <td valign="top">ContactName</td>
               <td valign="top"><a href="mailto:ContactUser@gmail.com">ContactUser@gmail.com</a></td>
            </tr>
            <tr class="tab_bg_2">
                <td valign="top"><a id="User_695_695" href="/dailyTickets/front/contacts.form.php?id=695">iLgOP</a></td>
                <td valign="top">ihi</td>
                <td valign="top"><a href="mailto:yassYassine@gmail.com">yassYassine@gmail.com</a></td>
             </tr>
       </tbody>
    </table>

我想要的是删除(trtable)中每个第一个td中的a元素,然后将删除的元素提供给第二个td元素(trtable),就像这样:

  <tr class="tab_bg_1">
                   <td valign="top">Gov0N</td> // delete the a element from here , but keep the inside text  
                   <td valign="top"><a id="User_245_245" href="/dailyTickets/front/contacts.form.php?id=245">ContactName</a></td> // add it here , around the text
                .........
   </tr>
   <tr class="tab_bg_2">
                    <td valign="top">iLgOP</td>
                    <td valign="top"><a id="User_695_695" href="/dailyTickets/front/contacts.form.php?id=695">ihi</a></td>
                   .....

    </tr>

预先提交^^

1 个答案:

答案 0 :(得分:2)

$('tr').each(function() {
    var link = $(this).find('td:first a');
    text_of_second_td = $(this).find('td:nth-child(2)').text();
    $(this).find('td:first').html(link.text()); //Link's text to the first td
    link.text(text_of_second_td); //Replace Link's text by the second td's text
    $(this).find('td:nth-child(2)').html(link); //Link to the second td       
});

http://jsbin.com/afutir/1/