如何使用javascript将内容从表的一行移动到另一行
How to move content from one row of the table to another table row using javascript
我有两个表。表1和表2。我想复制一行内容到另一行时,点击行而不是按钮,使用java-script。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("t1").clone().appendTo("t2");
});
});
</script>
</head>
<body>
<table style="width:100%;border-style:dashed">
<tr1>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr1>
</table>
</br>
<table style="width:100%;border-style:dashed">
<tr2>
<th></th>
<th></th>
<th></th>
</tr2>
</table>
<button>colne</button>
</body>
</html>
如果能给出相互依存的例子,我将不胜感激。
您可以尝试使用
来实现这一点.droppable
检查这个
Jsfiddle
$(document).ready(function() {
var items = [];
$("#myTable tr").on("click", function() {
var newTr = $(this).closest("tr").clone();
var newButtonHTML = "<input type = 'button' value='Edit' onclick='Edit()' /><input type='button' value='Delete' onclick='deleteRow(this.parentNode.parentNode.rowIndex)'/>";
$(newButtonHTML).children("button").click(function(e) {
});
$(newTr).children("td:last").html("").html(newButtonHTML);
items.push(newTr);
newTr.appendTo($("#stopsTable"));
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div style="height: 700px;width: 100%;overflow: auto; float: left;">
<table id="myTable" border="1px" style="width: 24%; font-size: 10px; float :left;" >
<thead>
<tr>
<th>S No</th>
<th>Stop Name</th>
<th>Longitude</th>
<th>Latitude</th>
<th>ETA</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr >
<td >1</td>
<td>The Indian Heights School</td>
<td>28.56137</td>
<td>77.05249</td>
<td>06:06:12</td>
<td>Ignition_On</td>
</tr>
<tr >
<td >2</td>
<td>The Indian Heights School</td>
<td>28.56136</td>
<td>77.05246</td>
<td>06:06:23</td>
<td>Ignition_On</td>
</tr>
<tr >
<td >3</td>
<td>The Indian Heights School</td>
<td>28.56135</td>
<td>77.05242</td>
<td>06:06:31</td>
<td>Start</td>
</tr>
<tr >
<td >4</td>
<td>The Indian Heights School</td>
<td>28.56139</td>
<td>77.05245</td>
<td>06:06:41</td>
<td>Ignition_On</td>
</tr>
</tbody>
</table>
<table id="stopsTable" border="1px" style="width: 24%; margin-left: 10px; font-size: 10px; float :left;">
<thead>
<tr>
<th>S No</th>
<th>Stop Name</th>
<th>Longitude</th>
<th>Latitude</th>
<th>ETA</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
看看我的代码在这个例子中,我想它会对你和其他朋友有帮助。
应该是一个简单的jquery dom操作,考虑如下:
<table id="first-table">
<tbody>
<tr class="row-copier"><td>Content here</td><td>other content</td></tr>
<tr class="row-copier"><td>more content</td><td>second column</td></tr>
<tr class="row-copier"><td>...and something completely different</td><td><h1>YAY!</h1></td></tr>
</tbody>
</table>
<table id="second-table">
<tbody>
<tr><td>Result Table</td><td>where stuff will get copied</td></tr>
</tbody>
</table>
<script>
jQuery(function($) {
$('.row-copier').click(function() {
// select row-copier row, clone it, and append to second table body
$(this).clone().appendTo('#second-table>tbody');
});
});
</script>
重要的位是clone
操作,用于进行深度复制,appendTo
用于指定克隆副本的放置位置。我编辑了答案,以反映您希望复制行点击行本身的任何地方,因此不需要链接,也不需要closest
来获取行。
祝你好运!
相关文章:
- 如何根据单元格中的值将一行从一个谷歌电子表格移动到另一个
- 若我知道移动设备上的屏幕大小,该如何分隔一行文字
- Onclick 无法正常工作,并且将按钮移动到下一行不起作用
- 我如何创建一个函数,以便编程行将等到它完成并移动到 javascript 中的下一行
- 单击按钮时,验证是否必须使用 Jquery 将至少一行移动到自定义表中
- 如何在上下移动时免除第一行,或者如何自动排列编号
- JQuery移动页脚项不在一行显示
- 找出您刚刚移动了表的哪一行
- CSS:当光标向下移动时,避免跳过一行
- 在使用可能的计时器检测到输入后自动移动到下一行
- Jquery移动到下一行's输入元素的id
- 如何将光标的焦点从一行移动到另一行
- 如何保留组合框选定的背景颜色,内部网格面板一旦移动到下一行.或者可以说集中注意力
- IE9悬停bug -悬停状态在移动一行后保持不变
- 如何在一行中一个接一个地移动图像
- 如何使用javascript将内容从表的一行移动到另一行
- 使用jQuery移动(高亮显示)HTML表中的最后一行
- Ajax从行中删除单元格,重新计算行,将单元格移动到下一行
- HTML Div移动文本到下一行
- 调整窗口大小会导致按钮在下一行移动