Pass object through dataTransfer

前端 未结 2 683
你的背包
你的背包 2021-01-31 08:16

I\'m trying to figure out a way to pass a native object through javascript\'s event.dataTransfer for drag and drop. I\'m writing the front end editor portion of a CMS, and want

2条回答
  •  不知归路
    2021-01-31 08:17

    You should pass the object to JSON.stringify before using setData because you can only store strings.

    var j = JSON.stringify(foo);
    e.originalEvent.dataTransfer.setData("foo", j);
    

    And the other way round you have to reconvert the string to an object:

    var obj = JSON.parse(e.originalEvent.dataTransfer.getData("foo"));
    console.log("foo is:", obj);
    

    See this working Fiddle

提交回复
热议问题