2014-02-22 29 views
1

我想在拖放事件處理程序中獲取拖動元素的ID javascript如何在javascript中獲取拖動元素的id?

div與id place5是可拖動的,拖放位置是另一個ID爲dropArea的div。

在drop事件處理程序中如何獲取div(place5)的id?

<html> 
<head> 

<style> 
.mainArea{ 
     width: 500px; 
      height : 500px; 
      border-color:red; 
      border-style:solid; 
      border-width:5px; 
} 
</style> 
<script> 
    function dragEventHandler(theEvent) { 
     theEvent.dataTransfer.setData("Text", theEvent.target.id); 
     //Some code here. 
    } 
    function dropEventHandler(theEvent) { 
     //how to get the id of div with id "place5" here? 
    } 
</script> 
</head> 
<body> 
<div id="dropArea" class="mainArea" ondrop="dropEventHandler(event);" ondragover="event.preventDefault();> 

</div> 

    <div id="place5" draggable="true" ondragstart="dragEventHandler(event);"> 
    </div> 
    </body> 
    </html> 
+1

你嘗試過這麼遠嗎?請使用您現在嘗試使用的代碼編輯您的問題。 –

+1

可能重複的[我如何得到一個拖曳的元素和綁定時,Id的滴ID?](http://stackoverflow.com/questions/13139681/how-can-i-get-the-id-of-an- drag-element-and-bind-while-dropping) – itsazzad

+0

var draggedID = ui.draggable.attr('id'); – itsazzad

回答

0

執行拖動啓動事件過程中的相反操作。

所以上的dragstart您有:

function dragEventHandler(theEvent) { 
    theEvent.dataTransfer.setData("Text", theEvent.target.id); 
    //Some code here. 
} 

因此在降,你將有:

function dropEventHandler(theEvent) { 
    var id = theEvent.dataTransfer.getData("Text"); 
    //Other code here. 
} 
相關問題