{"id":1419,"date":"2011-02-09T20:58:53","date_gmt":"2011-02-09T12:58:53","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1419"},"modified":"2018-07-24T11:09:42","modified_gmt":"2018-07-24T03:09:42","slug":"html5-drag-drop-%e6%8b%96%e6%8b%bd%e4%b8%8e%e6%8b%96%e6%94%be%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2011\/02\/html5-drag-drop-%e6%8b%96%e6%8b%bd%e4%b8%8e%e6%8b%96%e6%94%be%e7%ae%80%e4%bb%8b\/","title":{"rendered":"HTML5 drag &amp; drop \u62d6\u62fd\u4e0e\u62d6\u653e\u7b80\u4ecb"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a><br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1419\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=1419<\/a><\/p>\n<h3>\u4e00\u3001\u524d\u9762\u7684\u8bdd<\/h3>\n<p>HTML5\u63d0\u4f9b\u4e13\u95e8\u7684\u62d6\u62fd\u4e0e\u62d6\u653e\u7684API\uff0c\u4ee5\u540e\u5b9e\u73b0\u8fd9\u7c7b\u6548\u679c\u5c31\u4e0d\u5fc5\u4e71\u6298\u817e\u4e86\u3002\u4f46\u662f\uff0c\u8003\u8651\u5230Opera\u6d4f\u89c8\u5668\u4f3c\u4e4e\u5bf9\u6b64\u4e0d\u611f\u5192\uff0c\u5728\u901a\u7528\u6027\u4e0a\u6709\u5f85\u5546\u69b7\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e5f\u5c31\u7b80\u5355\u8bf4\u4e00\u8bf4\u3002<\/p>\n<h3>\u4e8c\u3001\u76f8\u5173\u91cd\u70b9<\/h3>\n<ol>\n<li><span style=\"font-weight: bold;\">DataTransfer<\/span> \u5bf9\u8c61\uff1a\u9000\u62fd\u5bf9\u8c61\u7528\u6765\u4f20\u9012\u7684\u5a92\u4ecb\uff0c\u4f7f\u7528\u4e00\u822c\u4e3aEvent.dataTransfer\u3002<\/li>\n<li><span style=\"font-weight: bold;\">draggable<\/span> \u5c5e\u6027\uff1a\u5c31\u662f\u6807\u7b7e\u5143\u7d20\u8981\u8bbe\u7f6edraggable=true\uff0c\u5426\u5219\u4e0d\u4f1a\u6709\u6548\u679c\uff0c\u4f8b\u5982\uff1a\n<div class=\"zxx_code\">\n<pre>&lt;div title=\"\u62d6\u62fd\u6211\" draggable=\"true\">\u5217\u88681&lt;\/div><\/pre>\n<\/div>\n<\/li>\n<li><span style=\"font-weight: bold;\">ondragstart<\/span> \u4e8b\u4ef6\uff1a\u5f53\u62d6\u62fd\u5143\u7d20\u5f00\u59cb\u88ab\u62d6\u62fd\u7684\u65f6\u5019\u89e6\u53d1\u7684\u4e8b\u4ef6\uff0c\u6b64\u4e8b\u4ef6\u4f5c\u7528\u5728\u88ab\u62d6\u66f3\u5143\u7d20\u4e0a<\/li>\n<li><span style=\"font-weight: bold;\">ondragenter<\/span> \u4e8b\u4ef6\uff1a\u5f53\u62d6\u66f3\u5143\u7d20\u8fdb\u5165\u76ee\u6807\u5143\u7d20\u7684\u65f6\u5019\u89e6\u53d1\u7684\u4e8b\u4ef6\uff0c\u6b64\u4e8b\u4ef6\u4f5c\u7528\u5728\u76ee\u6807\u5143\u7d20\u4e0a<\/li>\n<li><span style=\"font-weight: bold;\">ondragover<\/span> \u4e8b\u4ef6\uff1a\u62d6\u62fd\u5143\u7d20\u5728\u76ee\u6807\u5143\u7d20\u4e0a\u79fb\u52a8\u7684\u65f6\u5019\u89e6\u53d1\u7684\u4e8b\u4ef6\uff0c\u6b64\u4e8b\u4ef6\u4f5c\u7528\u5728\u76ee\u6807\u5143\u7d20\u4e0a<\/li>\n<li><span style=\"font-weight: bold;\">ondrop<\/span> \u4e8b\u4ef6\uff1a\u88ab\u62d6\u62fd\u7684\u5143\u7d20\u5728\u76ee\u6807\u5143\u7d20\u4e0a\u540c\u65f6\u9f20\u6807\u653e\u5f00\u89e6\u53d1\u7684\u4e8b\u4ef6\uff0c\u6b64\u4e8b\u4ef6\u4f5c\u7528\u5728\u76ee\u6807\u5143\u7d20\u4e0a<\/li>\n<li><span style=\"font-weight: bold; \">ondragend<\/span> \u4e8b\u4ef6\uff1a\u5f53\u62d6\u62fd\u5b8c\u6210\u540e\u89e6\u53d1\u7684\u4e8b\u4ef6\uff0c\u6b64\u4e8b\u4ef6\u4f5c\u7528\u5728\u88ab\u62d6\u66f3\u5143\u7d20\u4e0a<\/li>\n<li><span style=\"font-weight: bold;\">Event.preventDefault()<\/span> \u65b9\u6cd5\uff1a\u963b\u6b62\u9ed8\u8ba4\u7684\u4e9b\u4e8b\u4ef6\u65b9\u6cd5\u7b49\u6267\u884c\u3002\u5728ondragover\u4e2d\u4e00\u5b9a\u8981\u6267\u884cpreventDefault()\uff0c\u5426\u5219ondrop\u4e8b\u4ef6\u4e0d\u4f1a\u88ab\u89e6\u53d1\u3002\u53e6\u5916\uff0c\u5982\u679c\u662f\u4ece\u5176\u4ed6\u5e94\u7528\u8f6f\u4ef6\u6216\u662f\u6587\u4ef6\u4e2d\u62d6\u4e1c\u897f\u8fdb\u6765\uff0c\u5c24\u5176\u662f\u56fe\u7247\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u7684\u52a8\u4f5c\u662f\u663e\u793a\u8fd9\u4e2a\u56fe\u7247\u6216\u662f\u76f8\u5173\u4fe1\u606f\uff0c\u5e76\u4e0d\u662f\u771f\u7684\u6267\u884cdrop\u3002\u6b64\u65f6\u9700\u8981\u7528\u7528document\u7684ondragover\u4e8b\u4ef6\u628a\u5b83\u76f4\u63a5\u5e72\u6389\u3002<\/li>\n<li><span style=\"font-weight: bold;\">Event.effectAllowed<\/span> \u5c5e\u6027\uff1a\u5c31\u662f\u62d6\u62fd\u7684\u6548\u679c\u3002<\/li>\n<\/ol>\n<h3>\u4e09\u3001\u76f8\u5173\u5b9e\u4f8b<\/h3>\n<p>\u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\u4e0a\u9762\u7684\u7c97\u4f53\u7684\u4e8b\u4ef6\u554a\u5bf9\u8c61\u554a\u4ec0\u4e48\u7684\uff0c\u505a\u4e86\u4e2a\u5f88\u7b80\u5355\u7684demo\u9875\u9762\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" target=\"_blank\" href=\"http:\/\/www.zhangxinxu.com\/study\/201102\/html5-drag-and-drop.html\">HTML5 drag &amp; drop\u5220\u9664\u5143\u7d20demo<\/a><\/p>\n<p>\u6b64demo\u5b9e\u73b0\u7684\u6548\u679c\u662f\u628a\u53f3\u4fa7\u7684\u5217\u8868\u62d6\u52a8\u5230\u5de6\u4fa7\u7684\u5199\u7740\u679c\u6562\u7684\u201c\u5783\u573e\u7bb1\u201d\u4e09\u4e2a\u5b57\u7684div\u5c42\u4e0a\uff0c\u6b64\u5217\u8868\u5143\u7d20\u5c31\u4f1a\u4ece\u8fd9\u4e2a\u661f\u7403\u4e0a\u6d88\u5931\u3002\u5982\u4e0b\u622a\u56fe\u8fde\u73af\u753b\u6240\u793a\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"\u5217\u8868\u5143\u7d20\u62d6\u62fd\u5220\u9664\u622a\u56fe\u4e4b\u9f20\u6807\u7ecf\u8fc7 \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201102\/2011-02-09_201525.png\" title=\"\u5217\u8868\u5143\u7d20\u62d6\u62fd\u5220\u9664\u622a\u56fe\u4e4b\u9f20\u6807\u7ecf\u8fc7 \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" class=\"alignnone\" width=\"469\" height=\"289\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"html5 drag &#038; drop ondragstart\u622a\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201102\/2011-02-09_201916.png\" title=\"html5 drag &#038; drop ondragstart\u622a\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" class=\"alignnone\" width=\"458\" height=\"290\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"html5 drag &amp; drop \u62d6\u62fd\u4e0e\u62d6\u653e\u7ecf\u8fc7\u76ee\u6807\u5143\u7d20\u622a\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201102\/2011-02-09_202158.png \" title=\"html5 drag &amp; drop \u62d6\u62fd\u4e0e\u62d6\u653e\u7ecf\u8fc7\u76ee\u6807\u5143\u7d20\u622a\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" class=\"alignnone\" width=\"458\" height=\"295\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"HTML5 drag drop demo ondrop\u622a\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201102\/2011-02-09_202601.png\" title=\"HTML5 drag drop demo ondrop\u622a\u56fe \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" class=\"alignnone\" width=\"451\" height=\"332\" \/><\/p>\n<p><strong>\u6e90\u4ee3\u7801\u5c55\u793a<\/strong><br \/>\n\u5176\u4e2dHTML\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;div class=\"dustbin\">&lt;br \/>\u5783&lt;br \/>\u573e&lt;br \/>\u7bb1&lt;\/div>\n&lt;div class=\"dragbox\">\n    &lt;div class=\"draglist\" title=\"\u62d6\u62fd\u6211\" draggable=\"true\">\u5217\u88681&lt;\/div>\n    &lt;div class=\"draglist\" title=\"\u62d6\u62fd\u6211\" draggable=\"true\">\u5217\u88682&lt;\/div>\n    &lt;div class=\"draglist\" title=\"\u62d6\u62fd\u6211\" draggable=\"true\">\u5217\u88683&lt;\/div>\n    &lt;div class=\"draglist\" title=\"\u62d6\u62fd\u6211\" draggable=\"true\">\u5217\u88684&lt;\/div>\n    &lt;div class=\"draglist\" title=\"\u62d6\u62fd\u6211\" draggable=\"true\">\u5217\u88685&lt;\/div>\n    &lt;div class=\"draglist\" title=\"\u62d6\u62fd\u6211\" draggable=\"true\">\u5217\u88686&lt;\/div>\n&lt;\/div>\n&lt;div class=\"dragremind\">&lt;\/div><\/pre>\n<\/div>\n<p>JS\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var $ = function(selector) {\n    <span style=\"color:green;\">\/*\u7b80\u5355\u7684\u9009\u62e9\u5668\u65b9\u6cd5*\/\n    ...<\/span>\n};\n\nvar eleDustbin = $(\".dustbin\")[0], eleDrags = $(\".draglist\"), lDrags = eleDrags.length, eleRemind = $(\".dragremind\")[0], eleDrag = null;\nfor (var i=0; i&lt;lDrags; i+=1) {\n    eleDrags[i].onselectstart = function() {\n        return false;\n    };\n    eleDrags[i].ondragstart = function(ev) {\n        <span style=\"color:green;\">\/*\u62d6\u62fd\u5f00\u59cb*\/<\/span>\n        <span style=\"color:green;\">\/\/\u62d6\u62fd\u6548\u679c<\/span>\n        ev.dataTransfer.effectAllowed = \"move\";\n        ev.dataTransfer.setData(\"text\", ev.target.innerHTML);\n        ev.dataTransfer.setDragImage(ev.target, 0, 0);\n        eleDrag = ev.target;\n        return true;\n    };\n    eleDrags[i].ondragend = function(ev) {\n        <span style=\"color:green;\">\/*\u62d6\u62fd\u7ed3\u675f*\/<\/span>\n        ev.dataTransfer.clearData(\"text\");\n        eleDrag = null;\n        return false\n    };\n}\neleDustbin.ondragover = function(ev) {\n    <span style=\"color:green;\">\/*\u62d6\u62fd\u5143\u7d20\u5728\u76ee\u6807\u5143\u7d20\u5934\u4e0a\u79fb\u52a8\u7684\u65f6\u5019*\/<\/span>\n    ev.preventDefault();\n    return true;\n};\n\neleDustbin.ondragenter = function(ev) {\n    <span style=\"color:green;\">\/*\u62d6\u62fd\u5143\u7d20\u8fdb\u5165\u76ee\u6807\u5143\u7d20\u5934\u4e0a\u7684\u65f6\u5019*\/<\/span>\n    this.style.color = \"#ffffff\";\n    return true;\n};\neleDustbin.ondrop = function(ev) {\n    <span style=\"color:green;\">\/*\u62d6\u62fd\u5143\u7d20\u8fdb\u5165\u76ee\u6807\u5143\u7d20\u5934\u4e0a\uff0c\u540c\u65f6\u9f20\u6807\u677e\u5f00\u7684\u65f6\u5019*\/<\/span>\n    if (eleDrag) {\n        eleRemind.innerHTML = '&lt;strong>\"' + eleDrag.innerHTML + '\"&lt;\/strong>\u88ab\u6254\u8fdb\u4e86\u5783\u573e\u7bb1';\n        eleDrag.parentNode.removeChild(eleDrag);\n    }\n    this.style.color = \"#000000\";\n    return false;\n};<\/pre>\n<\/div>\n<h3>\u56db\u3001\u5306\u5306\u7ed3\u8bed<\/h3>\n<p>\u4eca\u5929\u662f\u5e74\u540e\u7b2c\u4e00\u5929\u4e0a\u73ed\uff0c\u6ca1\u6709\u591a\u5c11\u60f3\u6298\u817e\u7684\u5fc3\u60c5\uff0c\u6240\u4ee5\uff0c\u4e00\u4e9b\u5570\u54e9\u5427\u55e6\u7684\u8bdd\u5c31\u7701\u4e86\u3002\u867d\u7136\u662fHTML5\u7684\u4e1c\u897f\uff0c\u4f46\u662f\uff0c\u6839\u636e<a href=\"http:\/\/www.findmebyip.com\/litmus#html5-web-applications\">findmebyip<\/a>\u4e2d\u663e\u793a\u7684\u6d4f\u89c8\u5668\u652f\u6301\u60c5\u51b5\u6765\u770b\uff0cIE6~IE8\u90fd\u662f\u6709drag &amp; drop API\u7684\uff08\u89c1\u4e0b\u622a\u56fe\uff09\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" alt=\"IE6~IE8\u4e5f\u662f\u6709drag&#038;drop API\u7684 \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201102\/2011-02-09_204935.png\" title=\"IE6~IE8\u4e5f\u662f\u6709drag&#038;drop API\u7684 \u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b\" class=\"alignnone\" width=\"564\" height=\"421\" \/><\/p>\n<p>\u6839\u636e\u81ea\u5df1\u7684\u7b80\u5355\u7684\u6d4b\u8bd5\uff0c\u4f4e\u7248\u672c\u7684IE\u6d4f\u89c8\u5668\u786e\u5b9e\u652f\u6301\u8bf8\u5982ondragstart\u4e8b\u4ef6\uff0c\u4f46\u662f\u4f1a\u62a5\u4e0d\u8ba4\u8bc6dataTransfer\u7684\u9519\u8bef\u3002\u53ef\u89c1IE\u5728\u7ec6\u8282\u7684\u5904\u7406\u4e0a\u4e0e\u73b0\u4ee3\u6d4f\u89c8\u5668\u6709\u4e9b\u4e0d\u540c\u3002\u4f46\u662f\uff0c\u76ee\u524d\u81ea\u5df1\u6ca1\u6709\u8fd9\u4e48\u591a\u7cbe\u6c14\u795e\u628aIE\u4e0b\u7684\u62d6\u62fd\u4e5f\u6298\u817e\u51fa\u6765\uff0c\u6545\u8bf7\u539f\u8c05\u76ee\u524ddemo\u5728IE\u4e0b\u662f\u7eb9\u4e1d\u4e0d\u52a8\uff0c\u6ca1\u6709\u6548\u679c\u4e5f\u4e0d\u62a5\u9519\u7684\u3002\u56de\u5934\u6709\u529f\u592b\uff0c\u4e00\u5b9a\u4f1a\u628aIE\u76f8\u5173\u7684\u4e9b\u4e1c\u897f\u8865\u4e0a\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\u3002\u6587\u4e2d\u8981\u662f\u6709\u8868\u8ff0\u4e0d\u51c6\u786e\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\uff0c\u4e3a\u4e86\u907f\u514d\u9648\u65e7\u77e5\u8bc6\u8bef\u5bfc\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1419\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=1419<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u4e86\u4e0b\u5229\u7528HTML5 drag &amp; drop API\u975e\u5e38\u7b80\u5355\u8f7b\u677e\u5730\u5b9e\u73b0\u62d6\u62fd\u4e0e\u62d6\u653e\u6548\u679c\u3002\u63d0\u4f9b\u4e00\u4e9b\u5173\u952e\u672f\u8bed\u7684\u8bb2\u89e3\uff0c\u63d0\u4f9b\u7b80\u5355\u5b9e\u7528\u7684demo\u9875\u9762\uff0c\u63d0\u4f9b\u5fc5\u8981\u7684\u6e90\u4ee3\u7801\u5c55\u793a\u548c\u6548\u679c\u622a\u56fe\u3002\u603b\u4e4b\uff0c\u5e0c\u671b\u80fd\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[215,560,561,52,132,137,140,347,139,212],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/1419"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1419"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/1419\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}