Bonjour,

Je voudrais bloquer le drop d'un élément dans une div, elle-même contenue dans une div qui accepte le drop.


Comment faire pour avoir ce comportement ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
 
<!DOCTYPE HTML>
<html>
 
<head>
<style>
#divA {width:350px;height:70px;padding:10px;border:1px solid #aaaaaa; margin-bottom: 5px; }
#divB {width:150px;height:70px;padding:10px;border:1px solid #62b333;}
#div1, #div2 { border: 1px dashed; margin-bottom: 5px; }
 
</style>
 
<script>
function allowDrop(ev)
{
ev.preventDefault();
}
 
function disableDrop(ev)
{
ev.returnValue = true;
}
 
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
 
function drop(ev)
{
ev.preventDefault();
var data = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
 
<body>
 
<div id="divA" ondrop="drop(event)" ondragover="allowDrop(event)" >
      <div id="div1">Zone droppable</div>
      <div id="div2">Zone non droppable</div>
</div>
 
<div id="divB" draggable="true" ondragstart="drag(event)" >
</div>
 
</body>
</html>
Merci de votre aide,