034.html (1775B)
1 <!doctype html> 2 <html> 3 <head> 4 <title>Drag and drop with cancelling dragenter on body</title> 5 <style type="text/css"> 6 div:first-child { 7 height: 100px; 8 width: 100px; 9 background: orange; 10 display: inline-block; 11 } 12 div:first-child + div { 13 height: 100px; 14 width: 100px; 15 margin-left: 100px; 16 background: blue; 17 display: inline-block; 18 } 19 </style> 20 <script type="text/javascript"> 21 window.onload = function () { 22 var drag = document.getElementsByTagName('div')[0], beforecount = 0, aftercount = 0, switchcount = false; 23 drag.ondragstart = function (e) { 24 e.dataTransfer.setData('text','hello'); 25 e.dataTransfer.effectAllowed = 'copy'; 26 }; 27 drag.ondragenter = drag.ondrop = drag.ondragover = function (e) { 28 e.preventDefault(); 29 }; 30 var drop = document.getElementsByTagName('div')[1]; 31 drop.ondragenter = function (e) { 32 switchcount = true; 33 }; 34 drop.ondragover = function (e) { 35 e.preventDefault(); 36 }; 37 document.body.ondragenter = function (e) { 38 if( e.target != this ) { return; } 39 e.preventDefault(); //don't cancel when it bubbles from the child elements 40 if( switchcount ) { aftercount++; } else { beforecount++; } 41 }; 42 drag.ondragend = function (e) { 43 document.getElementsByTagName('div')[2].innerHTML = ( beforecount == 1 && aftercount == 0 ) ? 'PASS' : ( 'FAIL, dragenter fired on body '+beforecount+' time(s) before blue.ondragenter and '+aftercount+' time(s) afterwards, instead of 1 and 0 times respectively' ); 44 }; 45 }; 46 </script> 47 </head> 48 <body> 49 50 <div draggable="true"></div><div></div> 51 <div> </div> 52 <p>Drag the orange square onto the blue square, then back to the orange square, and release it.</p> 53 <noscript><p>Enable JavaScript and reload</p></noscript> 54 55 </body> 56 </html>