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

查看:50
本文介绍了使用 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天全站免登陆