[英]how can get id value of a parent div at the time of dragging child div to another div
I am writing code of drag and drop. 我正在编写拖放代码。 I want to drag a child
div
from a parent div to another parent div, how can I get the value of id of parent div and the id of another parent div where the draggable child div are dropped . 我想将子
div
从父div拖动到另一个父div,如何获取父div的id值和放置可拖动子div的另一个父div的id。 I am writing code below. 我在下面写代码。
<script> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.target.id); ev.dataTransfer.setData("ter", $(this).closest("div")); var topi = $(this).closest("div"); console.log('draggable= ' + topi) } function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); var divid = ev.dataTransfer.getData("ter").toString(); // var divid=$(this).closest('ul').attr('id'); var topi = $(this).closest('div').attr('id'); console.log(data); console.log('sss ' + topi); ev.target.appendChild(document.getElementById(data)); $.ajax({ type: "POST", url: "insertData.action", data: data, success: function (response) { // console.log(data); } }); } </script>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"> <s:if test="inlist.size>0"> <% int k = 1; %> <s:iterator id="sl" value="inlist"> <div draggable="true" ondragstart="drag(event)" id='<s:property value="id"/>' width="88" height="31"> <li> <s:property value="name"/> </li> </div> </s:iterator> </s:if> </div> <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
and I am attaching the image 我附上图片
in that image every name like ganesh is under draggable child div and looking like box(total 3) is droppable parent div
. 在该图像中,每个像ganesh的名称都在可拖动的子div下,而看起来box(total 3)是可放置的父
div
。
In drag(ev)
, this
is the window
. 在
drag(ev)
, this
是window
。 Use $(ev.target).closest("div")
: 使用
$(ev.target).closest("div")
:
function drag(ev) {
var dragDivId = $(ev.target).closest("div")[0].parentElement.id;
console.log(dragDivId);
}
function drop(ev) {
var dropDivId = ev.target.id;
console.log(dropDivId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.