使用jquery将图像从一个表行复制到另一个表

时间:2011-12-30 01:10:23

标签: jquery jquery-selectors

我正在将内容从一个表复制到另一个表,如下所示;

var ntr='',//to store html for new table row
rows=[],//to collect new rows
$tbl=$("#table_rolecart tbody"),//original table
l=$("tr", $tbl).length;
var row;
for(int i=0;i=0;i<l;){
        row=$("tr:eq("+i+")", $tbl);
  ntr= '<tr><td>'
  +$("td:nth-child(2)",row).text()+'<input type=hidden name="access_id" value='+accessid+'></td><td>'    //add item name
  +$("td:nth-child(3)",row).text()+'</td><td>'  //add description
  //want to add the third column which is an image with img tag
  + '</td></tr>'

我目前正在做+ row.find(“img.role-action”),我想添加图像列,但表格显示的是[Object]而不是图像。如何将图像复制到新表?

原始表的内容如下

<table class="table sortable" id="table_rolecart">
    <tbody>
              <tr class="odd">
                  <td><a href="#"><img src="/gra/images/icons/fugue/cross-circle.png" class="move-row" alt="Remove"></a></td>
                  <td>agile_unix_role<input type="hidden" value="1101" name="role_id"></td>
                  <td>this agile unix role</td>
                  <td><img alt="add" src="/gra/images/icons/fugue/plus-circle.png" id="role_action" class="role_action"></td>
             </tr>
             <tr class="slider">
                  <td colspan="4"><div class="sliding">Business Justification:<input type="text" name="ar_businessjust"></div></td>
             </tr>
             <tr class="slider">
                  <td colspan="2"><div class="sliding">Start Date: <input type="text" id="ar_startdate" value=""></div></td> 
                  <td colspan="2"><div class="sliding">End Date: <input type="text" id="ar_enddate" value=""></div></td>
             </tr>
             <tr style="height:8px">
             </tr>
     </tbody>
</table>

我希望除最后一行之外的所有列的所有行组合成目标表中的一行到单独的列。我已经实现了这一点,唯一剩下的就是第一行第4列中的图像,我需要将其作为最后一列复制到目标表中

4 个答案:

答案 0 :(得分:1)

修改

我误解了......这就是你想要的:

var html = $('<div>').append($('img.role-action').clone()).remove().html();

上面将为您提供所选图像元素的HTML,包括元素本身。

答案 1 :(得分:1)

您需要使用$("td:nth-child(2)",row).html()代替$("td:nth-child(2)",row).text()

答案 2 :(得分:0)

我不确定我是否正确理解您的问题,但我在想,当您使用row.find(&#34; img.role-action&#34;)来获取img对象时,您可以使用以下内容打印图像:

'<img src="'+row.find("img.role-action").attr("src")+'" />'

您可能需要向图片标记添加更多属性,具体取决于您的需要。

答案 3 :(得分:-1)

您是否尝试过使用.html()来准确提取该单元格内的内容?