这是我的代码:
var dragged
document.addEventListener("drag", function (event) {
}, false)
document.addEventListener("dragstart", function (event) {
dragged = event.target
event.target.style.opacity = .5
}, false)
document.addEventListener("dragend", function (event) {
event.target.style.opacity = ""
}, false)
document.addEventListener("dragover", function (event) {
event.preventDefault()
}, false)
document.addEventListener("dragenter", function (event) {
if (event.target.className == "dropzone") {
event.target.style.background = "purple"
}
}, false)
document.addEventListener("dragleave", function (event) {
if (event.target.className == "dropzone") {
event.target.style.background = ""
}
}, false)
document.addEventListener("drop", function (event) {
event.preventDefault()
if (event.target.className == "dropzone") {
event.target.style.background = ""
dragged.parentNode.removeChild(dragged)
event.target.appendChild(dragged)
}
}, false)
#draggable {
width: 200px;
height: 20px;
text-align: center;
background: white;
}
.dropzone {
width: 200px;
height: 20px;
background: blueviolet;
margin-bottom: 10px;
padding: 10px;
}
<div class="dropzone ">
<div id="draggable" draggable="true" ondragstart="event.dataTransfer.setData('text/plain',null)">
this is draggable
</div>
</div>
<div class="dropzone"></div>
<div class="dropzone"></div>
<div class="dropzone"></div>
我想将文本放入其他div容器中。 drag text into other div
但是我发现我可以拖动div容器。就像这样
尽管设置draggable ='false',我仍然可以拖动div容器。
<div class="dropzone " draggable="false">
<div id="draggable" draggable="true"ondragstart="event.dataTransfer.setData('text/plain',null)">
this is draggable
</div>
</div><div class="dropzone" draggable="false">
</div><div class="dropzone" draggable="false">
</div><div class="dropzone" draggable="false"></div>
我使用chrome,但在控制台中看不到任何错误。
答案 0 :(得分:0)
您在user-select: none;
类中缺少.dropzone
。问题是,当您第一次拖动.dropzone时,它是在选择.dropzone和可拖动的文本。
在user-select: none;
上使用.dropzone
时,将不会选择dropzone div。
尝试一下。
var dragged
document.addEventListener("drag", function (event) {
}, false)
document.addEventListener("dragstart", function (event) {
dragged = event.target
event.target.style.opacity = .5
}, false)
document.addEventListener("dragend", function (event) {
event.target.style.opacity = ""
}, false)
document.addEventListener("dragover", function (event) {
event.preventDefault()
}, false)
document.addEventListener("dragenter", function (event) {
if (event.target.className == "dropzone") {
event.target.style.background = "purple"
}
}, false)
document.addEventListener("dragleave", function (event) {
if (event.target.className == "dropzone") {
event.target.style.background = ""
}
}, false)
document.addEventListener("drop", function (event) {
event.preventDefault()
if (event.target.className == "dropzone") {
event.target.style.background = ""
dragged.parentNode.removeChild(dragged)
event.target.appendChild(dragged)
}
}, false)
#draggable {
width: 200px;
height: 20px;
text-align: center;
background: white;
}
.dropzone {
width: 200px;
height: 20px;
background: blueviolet;
margin-bottom: 10px;
padding: 10px;
user-select: none;
}
<div class="dropzone ">
<div id="draggable" draggable="true" ondragstart="event.dataTransfer.setData('text/plain',null)">
this is draggable
</div>
</div>
<div class="dropzone"></div>
<div class="dropzone"></div>
<div class="dropzone"></div>