所有下降,HTML5拖放與只有JavaScript
我試圖使用事件委託,拖放和只有JavaScript。
當我從sourceContainer ....中雙擊項目1後,該項目將在destinationContainer中創建。這裏沒問題。
但是,當我嘗試拖動destinationContainer中的新項目時,我無法設置所選div的頂部和左側樣式。 event.target.id或this.id總是相同的。我期望id爲1,這是爲destinationContainer中存在的div創建的新ID。
任何人都可以幫助我瞭解如何才能夠更改只在destinationContainer中創建的div的頂部和左側樣式。
這裏是一個小提琴:https://jsfiddle.net/mdevera/oys3tLww/1/
這裏是降功能:
function drop(event) {
console.log("event.target.id: " + event.target.id);
console.log("this.id: " + this.id);
// This does not allow me to set the selected div's appropriate top and left styles.
// I do not want the whole destinationContainer to not move. Just the selected child element.
// var offset = event.dataTransfer.getData("text/plain").split(',');
//
// this.style.top = (event.clientY + parseInt(offset[0], 10)) + 'px';
// this.style.left = (event.clientX + parseInt(offset[1], 10)) + 'px';
}
希望幫助 –
我沒有使用你的例子一字不差。但是,我確實將源設置爲選定的li。出於某種原因,放置事件處理程序會將選定元素設置爲父容器(例如destinationContainer)。這意味着你不能使用this或event.target來確定選擇了哪個li元素。因此,我在drag_start函數中將source的值設置爲event.target。這就允許我在拖放功能中正確設置偏移集。 –