我對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.)
}
如果你的DOM元素在一個名爲變量'elem',那麼ID只是'elem.id'。 – jfriend00 2014-10-22 02:47:00
在這種情況下'數據'應該給你 – 2014-10-22 03:59:22
關於如何實現這一點的任何建議?嘗試了很多不同的東西,我的頭炸了。 – Jason 2014-10-22 04:08:39