微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html5 – 使用JSON拖放HTML 5 jQuery:e.dataTransfer.setData()

这是我的举动:
dragstart: function(e) {
    $(this).css('opacity','0.5');
    e.dataTransfer.effectAllowed = 'move';
    e.dataTransfer.setData('application/json',{
        id: $(this).attr('id'),header: $('header',this).text()
    });
},

我想传递一些信息,如id和文本.我的下落是:

drop: function(e) {
    var data = e.dataTransfer.getData('application/json');
    alert(data);
    $(this).attr('id',data.id);
    $('header',this).text(data.header);
},

但数据未定义,我无法访问我的数据.这是正确的方法吗?

谢谢!

解决方法

在拖动开始
var testjson = {name:"asd",tall:123};
e.dataTransfer.setData("text/plain",JSON.stringify(testjson));
e.dataTransfer.effectAllowed = "copy";

在下降

var data = e.dataTransfer.getData("text/plain");
console.log(JSON.parse(data));

你会得到的

Object {name: "asd",tall: 123}

在console.log中

原文地址:https://www.jb51.cc/html5/168482.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。