Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Jquery 将当前行添加到表中_Jquery_Html - Fatal编程技术网

Jquery 将当前行添加到表中

Jquery 将当前行添加到表中,jquery,html,Jquery,Html,我有一个html表的结构: <table id="test"> <thead> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </thead> <tbo

我有一个html表的结构:

    <table id="test">
     <thead>
       <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
       </tr>
     </thead>
     <tbody>
       <tr>
        <td><a href="javascript:{}" onclick="addPiece(1)">add</a></td>
       </tr>
       <tr>
        <td></td>
       </tr>
       <tr>
        <td></td>
       </tr>
       <tr>
        <td></td>
       </tr>
      </tbody>
     </table>


我需要在用户定义的表体位置克隆行。用户可以通过单击某个链接来定义克隆此块的位置。例如,
必须在表中第一个块之后克隆块。我如何才能做到这一点?

您需要字段的ID,然后插入行。然后你会:

$('#tr-id').after('<tr><td>New line</td></tr>');
$('#tr id')。在('New line')之后;
这个答案会有帮助。


实际上,在指定的答案中,它将行添加到表的末尾。但是,如果从用户事件中获取行,则不需要该选择器。

您可以在指定行之后或之前添加新行 在指定索引之前添加行的步骤

$("<tr><td>0</td></tr>").insertBefore( $('#test > tbody > tr').get(0) );
$("<tr><td>0</td></tr>").insertAfter( $('#test > tbody > tr').get(0) );
$(“0”).insertBefore($('#test>tbody>tr').get(0));
在指定索引后添加行的步骤

$("<tr><td>0</td></tr>").insertBefore( $('#test > tbody > tr').get(0) );
$("<tr><td>0</td></tr>").insertAfter( $('#test > tbody > tr').get(0) );
$(“0”).insertAfter($('#test>tbody>tr').get(0));

同样,您也可以使用.before()和.after()方法。

这里有一个非常简单的函数,它将创建一个
,在它里面放一个
,在td里面放一个5。然后它将把它添加到您的测试表中的tbody的末尾

function(){
    var tr = $('<tr/>');
    var td = $('<td/>');
    td.text('5');
    tr.append(td);
    $('#test tbody').append(​​​​​​​​​​​​​​​​​​tr);
}
但请注意,这需要两次查找#表,并且不能为您提供修改元素的句柄


(我在这里保存了这个示例,以便您可以使用它:)

例如,您希望在位置2之前添加您提到的块,那么这可能会对您有好处

function addPiece(position) {
    $("#YOURBLOCK").clone().insertBefore( $('#test > tbody > tr').get(position) );
}
插入后的情况类似

function addPiece(position) {
    $("#YOURBLOCK").clone().insertAfter( $('#test > tbody > tr').get(position) );
}

你能提供更多的细节吗?用户如何定义场所?用户如何定义空间?请提供一些额外的规范。你不需要一个ID,比如说,有很多很多方法可以找到一个元素:)谢谢你的回复。如果我需要在用户定义的位置克隆此块,该怎么办?用户可以通过单击某个链接来定义克隆此块的位置。例如,必须在表中第一个块之后克隆块。