Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript在表中插入行(子行)_Javascript_Jquery_Html - Fatal编程技术网

使用JavaScript在表中插入行(子行)

使用JavaScript在表中插入行(子行),javascript,jquery,html,Javascript,Jquery,Html,我的表包含4个主要行和一个(展开或折叠)按钮。单击(展开或折叠)按钮,我想通过迭代表行在所有行的中间再插入一行(总共8行)。如何使用Javascript执行此操作?请参见下图。有何建议 这是我在单击展开或折叠按钮时返回的代码 jQuery('#btnId').click(function() { var that = this; $("#example tbody tr").each(function(i) {

我的表包含4个主要行和一个(展开或折叠)按钮。单击(展开或折叠)按钮,我想通过迭代表行在所有行的中间再插入一行(总共8行)。如何使用Javascript执行此操作?请参见下图。有何建议

这是我在单击展开或折叠按钮时返回的代码

  jQuery('#btnId').click(function() {
            var that = this;
            $("#example tbody tr").each(function(i) {                   
                //what code need to add here
            });
        });

由于您没有提供代码示例,因此我只能建议一种实现方法

您可以通过
tr
上的id或css选择器(例如
:nth of type(4)
)来确定要插入的行上方的行

之后,您可以使用此行作为jquery元素(例如:
$(“tr#yourrow”)
),并使用
append()
在其后面追加一行

示例:
$(“tr#yourrow”).append(“…您的行定义…”)

根据更新后的问题:

jQuery('#btnId').click(function() {
    var that = this;
    $("#example tbody tr").each(function(i, object) {                   
        $(object).after("<tr>... your row definition ...</tr>")
    });
});
jQuery('#btnId')。单击(函数(){
var=这个;
$(“#示例tbody tr”)。每个(函数(i,对象){
$(对象)。在(“…您的行定义…”之后
});
});

行定义应由您自己完成。我不知道你案例中迭代背后的逻辑。但我想你会明白的。:)

@lonic谢谢你的回复..我已经添加了按钮点击的代码示例..可以告诉我如何插入行吗?需要迭代表格并在empid 030150旁边的employee id 03040旁边再插入一行,依此类推..我已经添加了另一个示例谢谢lonic..我尝试过了,但它被添加为地址栏旁边的列,而不是添加为新的列行作为新列添加Google“jquery将行插入表”