Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拖放;用JSON删除HTML5jQuery:e.dataTransfer.setData()_Html_Drag And Drop - Fatal编程技术网

拖放;用JSON删除HTML5jQuery:e.dataTransfer.setData()

拖放;用JSON删除HTML5jQuery:e.dataTransfer.setData(),html,drag-and-drop,Html,Drag And Drop,这是我的dragstart: 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() }); },

这是我的dragstart:

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);
},
但数据未定义,我无法访问我的数据。这条路对吗


谢谢你

如果在dragstart函数中包含允许的效应,例如:

e.dataTransfer.effectAllowed = 'move';
我的理解是,您需要在drop函数中定义等效的dropEffect:

e.dataTransfer.dropEffect = 'move';
拖动起动

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中,它是否与此相关?但是,这允许用户在默认情况下将json字符串放在任何-元素上,这可能不是有意的。如果有一种方法可以传递实际的json对象,那么会更方便。很遗憾,你不得不把它严格化。