使用jQuery插入表列 [英] Inserting a table column with jQuery

查看:100
本文介绍了使用jQuery插入表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态添加列所需的数据表。假设我有这个基本表开头:

I have a table of data that I need to dynamically add a column to. Lets say I have this basic table to start with:

<table>
 <tr><td>cell 1</td><td>cell 2</td><td>cell 3</td></tr>
 <tr><td>cell 1</td><td>cell 2</td><td>cell 3</td></tr>
 <tr><td>cell 1</td><td>cell 2</td><td>cell 3</td></tr>
</table>

我想在每一行的单元格1和单元格2之间插入一列......我我试过这个,但它没有像我期望的那样工作......

I would like to insert a column between cell 1 and cell 2 in each row... I've tried this but it just isn't working like I expect...

$(document).ready(function(){
 $('table').find('tr').each(function(){
  $(this).prepend('<td>cell 1a</td>');
 })
})


推荐答案

试试这个:

$(document).ready(function(){
    $('table').find('tr').each(function(){
        $(this).find('td').eq(0).after('<td>cell 1a</td>');
    });
});

您的原始代码会将列添加到每行的末尾,而不是列之间。这将找到第一列,并将单元格添加到第一列旁边。

Your original code would add the column to the end of each row, not in between columns. This finds the first column and adds the cell next to the first column.

这篇关于使用jQuery插入表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆