我想记住或获取当前拖动元素的数据.这就是我所做的:
$('.source_element').on('dragstart', function(e){
e.originalEvent.dataTransfer.setData("source", this);
});
$('.destination').on('drop', function(e){
var source = e.originalEvent.dataTransfer.getData('source');
console.log(source);
console.log(source.className);
console.log($(source));
$(this).html($(source).html());
e.preventDefault();
});
第一个console.log将[object HTMLDivElement]显示为字符串而不是对象,第二个未定义,第三个抛出错误.
所以我怀疑dataTransfer.setData仅接受数据作为字符串,不允许使用对象.如果是这样,您如何解决此问题,如何在不知道其特定ID的情况下记住被拖动的元素?
解决方法:
只需使用一些字符串来唯一地标识元素,例如为每个元素提供一个id或一个data属性
$('.source_element').on('dragstart', function(e){
var id = 'drag-'+(new Date()).getTime();
this.id = id;
//$(this).attr('data-drag', id);
e.originalEvent.dataTransfer.setData("source", id);
});
$('.destination').on('drop', function(e){
var source = e.originalEvent.dataTransfer.getData('source');
console.log(source);
console.log($('#'+source));
$(this).html($('#'+source).html());
//console.log($('[data-id="'+source+'"]'));
//$(this).html($('[data-id="'+source+'"]').html());
e.preventDefault();
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。