2014-10-22 78 views
5

我對jQuery相當陌生,並且試圖獲取拖動的圖像元素ID以追加到放置目標而不是圖像元素本身或放置目標ID。我正在使用html5原生DnD。到目前爲止,我可以通過使用drag函數中的datatransfer方法和drop中的getdata函數通過id發送元素來追加元素。每當我嘗試從下拉列表中調用該標識時,它將獲取放置目標標識而不是拖動的元素。獲取丟棄的元素ID而不是放棄目標ID

任何幫助將不勝感激,因爲我已經在網上徹底搜索,只有更多的方法來獲得放置區域的目標ID。這裏是我當前的代碼搗鼓的一個片段:

function allowDrop(ev) { 
ev.preventDefault(); 
} 

function dragStart(ev) { 

ev.dataTransfer.setData('Text/html', ev.target.id); // sends the dragged images data. 

//alert(ev.target.id); // alerts the correct id of the dragged image. 

} 


function drop(ev) { 

ev.preventDefault(); 
var data = ev.dataTransfer.getData("text/html");//retrieves dropped images data. 
ev.target.appendChild(document.getElementById(data));//this displays the dropped image. 

//alert(ev.target.id); // alerts the id of the drop target(Want to get the dropped images id.) 

//$("#mybillets").append("<span>"+ev.target.id+" </span>"); //appends the drop targets id(Want to append the dropped images id.) 
} 
+1

如果你的DOM元素在一個名爲變量'elem',那麼ID只是'elem.id'。 – jfriend00 2014-10-22 02:47:00

+0

在這種情況下'數據'應該給你 – 2014-10-22 03:59:22

+0

關於如何實現這一點的任何建議?嘗試了很多不同的東西,我的頭炸了。 – Jason 2014-10-22 04:08:39

回答

9

drop方法看起來像ev的事件對象,所以ev.target將指在其項目被丟棄的元素。因此使用ev.target.id來引用放置目標ID。

function allowDrop(ev) { 
 
    ev.preventDefault(); 
 
} 
 

 
function drag(ev) { 
 
    ev.dataTransfer.setData('Text/html', ev.target.id); 
 
} 
 

 
function drop(ev, target) { 
 
    ev.preventDefault(); 
 
    console.log(target.id, ev.target.id) 
 

 
    var data = ev.dataTransfer.getData("text/html"); 
 
    alert(data) 
 
}
#div1 { 
 
    width: 350px; 
 
    height: 70px; 
 
    padding: 10px; 
 
    border: 1px solid #aaaaaa; 
 
}
<div id="div1" ondrop="drop(event, this)" ondragover="allowDrop(event)"></div> 
 
<br/> 
 
<img id="drag1" src="//placehold.it/336X69/ff0000" draggable="true" ondragstart="drag(event)" width="336" height="69" />

+1

我實際上試圖顯示被拖動的圖像的id而不是放置目標,爲了更好地說明我正在嘗試做什麼,我編輯了一些代碼。 ev.target.id引用觸發該事件的元素,在放置事件的情況下,該元素引用圖像被放入的區域。我想獲取正在丟棄的圖像的ID。 – Jason 2014-10-22 03:57:56

+0

@Jason嘗試更新...它應該提醒'drag1' – 2014-10-22 04:09:37

+0

我做了,現在正在工作..謝謝!它現在附加正確的ID。 – Jason 2014-10-22 04:20:05

0

由於@Arun P佐尼。在這個簡單的例子上做得很好。

不過,我只是想補充一點,如果你嘗試從「A」標籤拖動,你就不會有太多的運氣。

這將工作(在所有瀏覽器):

<a draggable="true" ondragstart="drag(event)" id="drag1" href="#"> 
<img src="//placehold.it/336X69/ff0000" width="336" height="69" /> 
</a> 

但是這會工作(更多的瀏覽器):

<img draggable="true" ondragstart="drag(event)" id="drag1" src="//placehold.it/336X69/ff0000" width="336" height="69" /> 

這花了我很長的時間去發現了這一點,因爲許多瀏覽器不會返回或讓您獲得拖動的源代碼。 這個例子應該透露給你的源ID中的警報,並且控制檯:

<script> 
    function allowDrop(ev) { 
     ev.preventDefault(); 
    } 

    function drag(ev) { 
     ev.dataTransfer.setData('text', ev.target.id); 
    } 

    function drop(ev, target) { 
     ev.preventDefault(); 
     console.log(target.id + " : " + ev.target.id) 
     console.log(ev.dataTransfer.getData("text/html")); 
     console.log(ev.dataTransfer.getData("text")); 
     var data = ev.dataTransfer.getData("text"); 
     alert(data) 
    } 
</script> 
<style "type="text/css"> 
     #div1 
     { 
      width: 350px; 
      height: 70px; 
      padding: 10px; 
      border: 1px solid #aaaaaa; 
     } 
</style> 

<div id="div1" ondrop="drop(event, this)" ondragover="allowDrop(event)"></div> 
<br/> 
<img id="drag1" src="http://placehold.it/336X69/ff0000" draggable="true" ondragstart="drag(event)" width="336" height="69" /> 
+0

現在它設置在這個jsfiddle中:https://jsfiddle.net/tqk3b745/ – 2016-12-24 01:36:26