Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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_Html_Insert_Row - Fatal编程技术网

Javascript 将从一个表克隆的行插入另一个表的特定位置

Javascript 将从一个表克隆的行插入另一个表的特定位置,javascript,html,insert,row,Javascript,Html,Insert,Row,我有两个HTML表格;一个源地址表和一个目标地址表,我想在其中将行从源表移到目标表,反之亦然。 它基本上是工作的,但是当从源表向目标表插入一行时,我就卡住了。我不想追加一行,因为目标表中的最后一行支持一个文本字段,用户可以在其中手动键入地址。下面是当用户单击源地址表中的一行以删除该行并将其移动到目标表中时的处理程序函数: $("#source_christmas_lights_addresses .move-row").live("click", function() { var tr = $

我有两个HTML表格;一个源地址表和一个目标地址表,我想在其中将行从源表移到目标表,反之亦然。 它基本上是工作的,但是当从源表向目标表插入一行时,我就卡住了。我不想追加一行,因为目标表中的最后一行支持一个文本字段,用户可以在其中手动键入地址。下面是当用户单击源地址表中的一行以删除该行并将其移动到目标表中时的处理程序函数:

$("#source_christmas_lights_addresses .move-row").live("click", function() {

var tr = $(this).closest("#source_christmas_lights_addresses tbody tr").remove().clone();

//The below line of code has been removed because I don't want to add my row to the end of the table.
//$("#destination_christmas_lights_addresses tbody").append(tr);


//Below here is new code added to insert a row
var tableRef = document.getElementById('destination_christmas_lights_addresses').getElementsByTagName('tbody')[0];

//Insert a row into the destination table before the end of route address.
var newRow   = tableRef.insertRow(tableRef.rows.length -1);

//Insert a cell in the row at index 0
var newCell  = newRow.insertCell(0);
newCell.innerHTML = "I want inner HTML from variable tr here";

});
问题1 当我使用append函数(注释掉)时,除了我不想要的行位于表的底部之外,其他都可以正常工作。 使用insertRow函数时,我无法确定如何移动tr变量的适当元素来创建新行。我需要innerHTML,因为它包含允许将行移回源表的代码。 谁来帮忙

问题2我确信以下代码行的编写方式可能不同

var tableRef = document.getElementById('destination_christmas_lights_addresses').getElementsByTagName('tbody')[0];
我想我可以用:

var tableRef = $("#destination_christmas_lights_addresses tbody");
但我不能让它工作

感谢您的帮助

谢谢大家的回复。解决这两个问题的最终解决方案如下

$("#source_christmas_lights_addresses .move-row").live("click", function() {

var tr = $(this).closest("#source_christmas_lights_addresses tbody tr").remove().clone();

//reference id of element representing the row I want to add the address before.
$("#trip_planner_end_address_row").before(tr);

      });
可能问题是“删除方法和克隆”,您应该将克隆的tr保存在一个变量中,然后删除原始tr。 像这样:

var tr_original = $(this).closest("#source_christmas_lights_addresses tbody tr");
var tr_cloned = tr_original.clone();
tr_original.remove();
我想看看您的原始示例代码,这样就很容易理解真正的问题;) 祝你好运

试试这个

var $WhereToAppend=('#destination_christmas_lights_addresses').find('tr:first');

($tr).insertBefore($WhereToAppend);

谢谢你的回复。从您的回答中,我意识到还有一些其他功能(例如InsertBefore)我没有尝试过。当我使用jquery时,我所需要做的就是添加以下行并引用我想在前面输入地址的元素的id。解决方案如下:$(“#行程"计划"结束"地址"行)之前(tr);