拖拽div的值到table
Html 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQeruyUI拖拽效果_拖拽div的值到table表格里面1</title>
<!--导入jquery插件-->
<script type="text/javascript" src="js/jQuery1.11.3.min.js"></script>
<!--导入jqueryUI插件-->
<script type="text/javascript" src="js/jquery-ui.js"></script>
<style>
table {
border-collapse: collapse;
}
table td {
empty-cells: show
}
#table-tab {
float: left;
width: 600px;
height: 300px;
}
#table-tab table {
width: 800px;
height: 300px;
display: none;
}
#table-tab table tr {
border: 1px solid #666666;
}
#table-tab table tr td {
width: 500px;
border: 1px solid #666666;
}
#table-tab table tr th{
border: 1px solid #666666;
}
.divPart{
width: 120px;
height: 30px;
line-height: 30px;
background-color: green;
border: 1px solid white;
}
#precessText{
width:200px;
height:200px;
text-align: center;
line-height: 36px;
float: left;
/* overflow-x: hidden; */
}
</style>
</head>
<body>
<h2>拖拽div的值到table表格里面</h2>
<div id="precessText"></div>
<div id="table-tab">
<table style="float:left;display: block">
<tr height="40">
<th width="150">2018/10/08 - 2018/10/14</th>
<th width="150">2018/10/15 - 2018/10/21</th>
<th width="150">2018/10/22 - 2018/1028</th>
<th width="150">周一</th>
</tr>
<tr height="40">
<td width="150" id="target11"></td>
<td width="150" id="target12"></td>
<td width="150" id="target13"></td>
<td width="150" id="target14"></td>
</tr>
<tr height="40">
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
</tr>
<tr height="40">
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
</tr>
<tr height="40">
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
</tr>
<tr height="40">
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
</tr>
<tr height="40">
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
</tr>
</table>
<table style="float:left">
<tr height="40">
<th width="150">2018/11/05 - 2018/11/11</th>
<th width="150">2018/11/05 - 2018/11/11</th>
<th width="150">2018/11/12 - 2018/11/18</th>
<th width="150">周二</th>
</tr>
<tr height="40">
<td width="150" id="target11"></td>
<td width="150" id="target12"></td>
<td width="150" id="target13"></td>
<td width="150" id="target14"></td>
</tr>
<tr height="40">
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
</tr>
<tr height="40">
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
</tr>
<tr height="40">
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
</tr>
<tr height="40">
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
</tr>
<tr height="40">
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
</tr>
</table>
<table style="float:left">
<tr height="40">
<th width="150">2018/11/20 - 2018/11/11</th>
<th width="150">2018/11/30 - 2018/11/11</th>
<th width="150">2018/11/10 - 2018/11/11</th>
<th width="150">周三</th>
</tr>
<tr height="40">
<td width="150" id="target11"></td>
<td width="150" id="target12"></td>
<td width="150" id="target13"></td>
<td width="150" id="target14"></td>
</tr>
<tr height="40">
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
<td width="150" id="target21"></td>
</tr>
<tr height="40">
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
<td width="150" id="target31"></td>
</tr>
<tr height="40">
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
<td width="150" id="target41"></td>
</tr>
<tr height="40">
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
<td width="150" id="target51"></td>
</tr>
<tr height="40">
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
<td width="150" id="target61"></td>
</tr>
</table>
</div>
<div id="btn">
<button id="preve" onclick="preveTable()"><</button>
<button id="next" type="button" onclick="nextTable()">></button>
</div>
</body>
</html>
JS代码
//动态创建拖拽元素
var partArr = ['桩基', '承台', '箱梁', '盖梁', '桩基', '承台', '箱梁', '盖梁'];
for (var i = 0; i < 8; i++) {
var div = document.createElement('div');
div.id = "draggable" + i;
div.className = 'divPart';
div.title = 'zhuangJi';
div.innerText = partArr[i];
$('#precessText').append(div);
}
//切换table表格
var count = 0;
var tabTogggle = gettableList();
//切换到下一个
function nextTable() {
if (count == tabTogggle.length - 1) {
return;
}
count++;
tabTogggle[count - 1].style.display = 'none';
tabTogggle[count].style.display = 'block';
}
//切换到上一个
function preveTable() {
if (count == 0) {
return;
}
count--;
tabTogggle[count + 1].style.display = 'none';
tabTogggle[count].style.display = 'block';
}
//获取div下面的所有table
function gettableList() {
var table = document.getElementById('table-tab');
var tableList = removeWhiteNode(table.childNodes);
var arr = [];
for (var i = 0; i < tableList.length; i++) {
arr.push(tableList[i]);
}
return arr;
}
//去除空白文本节点
function removeWhiteNode(node) {
for (var i = 0; i < node.length; i++) {
if (node[i].nodeType === 3 && /^\s+$/.test(node[i].nodeValue)) {
node[i].parentNode.removeChild(node[i]);
}
}
return node;
}
//根据存放droggable的ID获取table表头的时间
function getTime(id) {
//获取到所有的tr
var getTable= gettableList();
var tabChildList = removeWhiteNode(getTable[0].childNodes);
var trList = removeWhiteNode(tabChildList[0].childNodes);
console.log(trList);
for (var i = 0; i < trList.length; i++) {
for (var j = 0; j < trList[i].childNodes.length; j++) {
if(id == trList[i].childNodes[j].id){
return trList[0].childNodes[j].innerHTML;
}
}
}
}
</script>
<!--自写脚本-->
<script type="text/javascript" language="javascript">
//在页面加载完之后加载jquery
$().ready(function (e) {
//拖拽复制体
$('div[id^="draggable"]').draggable({
revert: 'invalid' //未放到droppable上时,回到原位置
});
//释放后
$('td[id^="target"]').droppable({
drop: function (event, ui) {
var thValue = getTime(event.target.id);
console.log(thValue);
var source = ui.draggable;
$('.ui-draggable').css({ "left": "0px", "top": "0px", "text-align": "center" });
source.draggable("disable");
$('<img/>', {
src: 'img/btn_delete.png',
style: 'display:none',
click: function () {
source.draggable("enable");
$('#precessText').prepend(source);
$(this).remove();
}
}).appendTo(source);
source.mouseenter(function () {
$(this).find("img").show();
});
source.mouseleave(function () {
$(this).find("img").hide();
});
$(this).append(source);
},
});
});
</script>
<!--自写脚本-->