How can I write a javascript function with the if/else statement to check whether a <div> class contains a <img> or <img> class

huangapple go评论86阅读模式
英文:

How can I write a javascript function with the if/else statement to check whether a <div> class contains a <img> or <img> class

问题

I'm trying to make a web page utilizing the drag and drop feature. The page is meant for the user to drag an image (I made two classes, one called "sails" and the other "boats") to a div (class named "dropbox" and "dropbox2").

The problem I'm having is that if a user drops an image into a droppable div that already contains an image, one of the images disappears and can't be recovered.

To solve this problem, I believe I need to create a function using the if/else statement to check whether the div contains an image. If it does, then I want the drag and drop feature to keep its default setting so that it doesn't drop into the div and disappear from the user. Else, it executes the code I already have that turns the default nature off so that it can drop into the div.

if (**div contains an img**) {
  **keep the default nature so that it doesn't drop into the div**
} else {
  function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
  }
}

How can I word the condition so that I can check whether or not the div contains the image, and how can I word the block of code to NOT allow the image to drop into that div if the condition is true?

Or am I attacking this problem the wrong way? It seems logical to me, but I am not experienced.

Here is a link to my codepen for this project:

CodePen Link

Any help would be greatly appreciated.

英文:

Im trying to make a web page utilizing the drag and drop feature. The page is meant for the user to drag an image (I made two classes, one called "sails" and the other "boats") to a div (class named "dropbox" and "dropbox2").

The problem I'm having is that if a user drops an image into a droppable div that already contains an image, one of the images disappears and can't be recovered.

To solve this problem I believe I need to create a function using the if/else statement to check whether the div contains an image. If it does then I want the drag and drop feature to keep its default setting so that it doesn't drop into the div and disappear from the user. Else, it executes the code I already have that turns the default nature off so that it can drop into the div.

if (**div contains an img**) {
  **keep the default nature so that it doesn&#39;t drop into the div**
} else {
  function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData(&quot;text&quot;);
    ev.target.appendChild(document.getElementById(data));
}
}

How can I word the condition so that I can check whether or not the div contains the image and how can I word the block of code to NOT allow the image to drop into that div if the condition is true?

or am I attacking this problem the wrong way? It seems logical to me but I am not experienced.

Here is a link to my codepen for this project:

https://codepen.io/Pacman0006/pen/vYEWppe

Any help would be greatly appreciated How can I write a javascript function with the if/else statement to check whether a <div> class contains a <img> or <img> class

答案1

得分: 1

以下是您要翻译的代码部分:

// 当可拖动的 p 元素进入 droptarget 时,更改 DIVS 的边框样式
document.addEventListener("dragenter", function(event) {
  if (event.target.className == "dropbox" || event.target.className == "dropbox2") {
    event.target.style.border = "3px dotted red";
  }
});

// 当可拖动的 p 元素离开 droptarget 时,重置 DIVS 的边框样式
document.addEventListener("dragleave", function(event) {
  if (event.target.className == "dropbox" || event.target.className == "dropbox2") {
    event.target.style.border = "";
  }
});

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
  const element = ev.target.tagName === 'DIV' ? ev.target : ev.target.parentNode
  if (!element.children.length) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
  }
}
body {
  background-image: url('https://drive.google.com/uc?export=view&id=1EcpUOWcgSSuAsFv0GbbSRcMtaOH-UY8q');
  background-repeat: no-repeat;
}

.dropbox {
  position: absolute;
  float: ;
  width: 200px;
  height: 200px;
  border: 2px dashed black
}

.dropbox2 {
  position: absolute;
  float: ;
  width: 229px;
  height: 80px;
  border: 2px dashed black
}

.sails {
  transform: translateY(-5%);
}

#div1 {
  margin: 69px 354px;
}

#div2 {
  margin: 69px 620px;
}

#div3 {
  margin: 65px 887px;
}

#div4 {
  margin: 65px 1150px;
}

#div5 {
  margin: 367px 333px;
}

#div6 {
  margin: 367px 600px;
}

#div7 {
  margin: 362px 879px;
}

#div8 {
  margin: 363px 1150px;
}

#div9 {
  margin: 283px 333px;
}

#div10 {
  margin: 283px 599px;
}

#div11 {
  margin: 279px 866px;
}

#div12 {
  margin: 279px 1129px;
}

#div13 {
  margin: 581px 312px;
}

#div14 {
  margin: 581px 579px;
}

#div15 {
  margin: 576px 858px;
}

#div16 {
  margin: 577px 1129px;
}

#drag3 {
  transform: translateY(-54%);
}
<div class="dropbox2" id="div9" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox2" id="div10" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox2" id="div11" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox2" id="div12" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox2" id="div13" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox2" id="div14" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox2" id="div15" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox2" id="div16" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div4" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div5" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div6" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div7" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dropbox" id="div8" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<img class="sails" id="drag1" src="https://drive.google.com/uc?export=view&id=1ARGJDwdV9kH0IpOOL5hlm3lmOzOn81F1" draggable="true" ondragstart="drag(event)" width="215" height="210">
<img class="sails" id="drag2" src="https://drive.google.com/uc?export=view&id=1ARGJDwdV9kH0IpOOL5hlm3lmOzOn81F1" draggable="true" ondragstart="drag(event)" width="215" height="210">
<img id="drag3" src="https://drive.google.com/uc?export=view&id=1vZIEa2VkNaQLdb8zEWSkKaEeSLOOFBFV" draggable="true" ondragstart="drag(event)" width="290" height="170">

请注意,上述代码只是代码的翻译,不包含其他内容。

英文:

The first problem here is that ev.target.appendChild will add the image as a child to the div on the first drop, and it will append it to the img tag on the second drop, creating a deeper nested relationship each subsequent drop. This is observable by console.log(ev.target) in that function.

So one approach would be to first ensure that the node we are appending to is the div, and then our conditional will check if that div already has any children. I also simplified your event listeners. Here is an example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// When the draggable p element enters the droptarget, change the DIVS&#39;s border style
document.addEventListener(&quot;dragenter&quot;, function(event) {
if (event.target.className == &quot;dropbox&quot; || event.target.className == &quot;dropbox2&quot;) {
event.target.style.border = &quot;3px dotted red&quot;;
}
});
// When the draggable p element leaves the droptarget, reset the DIVS&#39;s border style
document.addEventListener(&quot;dragleave&quot;, function(event) {
if (event.target.className == &quot;dropbox&quot; || event.target.className == &quot;dropbox2&quot;) {
event.target.style.border = &quot;&quot;;
}
});
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData(&quot;text&quot;, ev.target.id);
}
function drop(ev) {
const element = ev.target.tagName === &#39;DIV&#39; ? ev.target : ev.target.parentNode
if (!element.children.length) {
ev.preventDefault();
var data = ev.dataTransfer.getData(&quot;text&quot;);
ev.target.appendChild(document.getElementById(data));
}
}

<!-- language: lang-css -->

body {
background-image: url(&#39;https://drive.google.com/uc?export=view&amp;id=1EcpUOWcgSSuAsFv0GbbSRcMtaOH-UY8q&#39;);
background-repeat: no-repeat;
}
.dropbox {
position: absolute;
float: ;
width: 200px;
height: 200px;
border: 2px dashed black
}
.dropbox2 {
position: absolute;
float: ;
width: 229px;
height: 80px;
border: 2px dashed black
}
.sails {
transform: translateY(-5%);
}
#div1 {
margin: 69px 354px;
}
#div2 {
margin: 69px 620px;
}
#div3 {
margin: 65px 887px;
}
#div4 {
margin: 65px 1150px;
}
#div5 {
margin: 367px 333px;
}
#div6 {
margin: 367px 600px;
}
#div7 {
margin: 362px 879px;
}
#div8 {
margin: 363px 1150px;
}
#div9 {
margin: 283px 333px;
}
#div10 {
margin: 283px 599px;
}
#div11 {
margin: 279px 866px;
}
#div12 {
margin: 279px 1129px;
}
#div13 {
margin: 581px 312px;
}
#div14 {
margin: 581px 579px;
}
#div15 {
margin: 576px 858px;
}
#div16 {
margin: 577px 1129px;
}
#drag3 {
transform: translateY(-54%);
}

<!-- language: lang-html -->

&lt;div class=&quot;dropbox2&quot; id=&quot;div9&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div10&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div11&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div12&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div13&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div14&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div15&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div16&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div1&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div2&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div3&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div4&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div5&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div6&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div7&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div8&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;img class=&quot;sails&quot; id=&quot;drag1&quot; src=&quot;https://drive.google.com/uc?export=view&amp;id=1ARGJDwdV9kH0IpOOL5hlm3lmOzOn81F1&quot; draggable=&quot;true&quot; ondragstart=&quot;drag(event)&quot; width=&quot;215&quot; height=&quot;210&quot;&gt;
&lt;img class=&quot;sails&quot; id=&quot;drag2&quot; src=&quot;https://drive.google.com/uc?export=view&amp;id=1ARGJDwdV9kH0IpOOL5hlm3lmOzOn81F1&quot; draggable=&quot;true&quot; ondragstart=&quot;drag(event)&quot; width=&quot;215&quot; height=&quot;210&quot;&gt;
&lt;img id=&quot;drag3&quot; src=&quot;https://drive.google.com/uc?export=view&amp;id=1vZIEa2VkNaQLdb8zEWSkKaEeSLOOFBFV&quot; draggable=&quot;true&quot; ondragstart=&quot;drag(event)&quot; width=&quot;290&quot; height=&quot;170&quot;&gt;

<!-- end snippet -->

答案2

得分: 1

你需要将条件写在drop函数内部。

以下是可能的实现方式:

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    
    // 检查目标容器是否已经有图片
    var targetHasImage = ev.target.querySelector('img');
    
    // 检查目标容器是否是图片
    var targetIsImage = ev.target.tagName === 'IMG';
    
    // 如果目标既没有图片也不是图片本身,就添加图片
    if (!targetHasImage && !targetIsImage) {
        ev.target.appendChild(document.getElementById(data));
    }
}

请注意,代码中的注释已经翻译成中文。

英文:

You need to write your condition in the drop function itself.

Here's how you might do it:

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData(&quot;text&quot;);
    
    // check if the target container has an image already
    var targetHasImage = ev.target.querySelector(&#39;img&#39;);
    
    // check if the target container is not an image
    var targetIsImage = ev.target.tagName === &#39;IMG&#39;
    
    // add the image if the target does not have an image and is not an image itself
    if (!targetHasImage &amp;&amp; !targetIsImage) {
        ev.target.appendChild(document.getElementById(data));
    }
}

答案3

得分: 0

在拖放事件中,当第一个元素被拖放时,请将一个新的classnoDrop附加到目标div上,以将其“标记”为已占用。

在第二次拖放时,请询问父元素是否具有class noDrop,如果有,则不执行任何操作,否则附加新对象。

英文:

At drop event, attach a new class : noDrop ev.target.classList.add(&quot;noDrop&quot;); to the target div when the first element is dropped, in order to "mark" him as occupied.

At the second drop ask the parent if it has the class noDrop, if it has, do nothing, otherwise attach the new object.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// When the draggable p element enters the droptarget, change the DIVS&#39;s border style
document.addEventListener(&quot;dragenter&quot;, function(event) {
if (event.target.className == &quot;dropbox&quot;) {
event.target.style.border = &quot;3px dotted red&quot;;
}
});
document.addEventListener(&quot;dragenter&quot;, function(event) {
if (event.target.className == &quot;dropbox2&quot;) {
event.target.style.border = &quot;3px dotted red&quot;;
}
});
// When the draggable p element leaves the droptarget, reset the DIVS&#39;s border style
document.addEventListener(&quot;dragleave&quot;, function(event) {
if (event.target.className == &quot;dropbox&quot;) {
event.target.style.border = &quot;&quot;;
}
});
document.addEventListener(&quot;dragleave&quot;, function(event) {
if (event.target.className == &quot;dropbox2&quot;) {
event.target.style.border = &quot;&quot;;
}
});
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData(&quot;text&quot;, ev.target.id);
}
function drop(ev) {
debugger
var parentID = ev.target.parentNode.className
var data = ev.dataTransfer.getData(&quot;text&quot;);
if (parentID.includes(&quot;noDrop&quot;)) {
console.log(&quot;no transfer&quot;);
ev.preventDefault();
} else {
ev.preventDefault();
ev.target.appendChild(document.getElementById(data));
ev.target.classList.add(&quot;noDrop&quot;);
}
}

<!-- language: lang-css -->

body {
background-image: url(&#39;https://drive.google.com/uc?export=view&amp;id=1EcpUOWcgSSuAsFv0GbbSRcMtaOH-UY8q&#39;);
background-repeat: no-repeat;
}
.dropbox {
position: absolute;
float: ;
width: 200px;
height: 200px;
border: 2px dashed black
}
.dropbox2 {
position: absolute;
float: ;
width: 229px;
height: 80px;
border: 2px dashed black
}
.sails {
transform: translateY(-5%);
}
#div1 {
margin: 69px 354px;
}
#div2 {
margin: 69px 620px;
}
#div3 {
margin: 65px 887px;
}
#div4 {
margin: 65px 1150px;
}
#div5 {
margin: 367px 333px;
}
#div6 {
margin: 367px 600px;
}
#div7 {
margin: 362px 879px;
}
#div8 {
margin: 363px 1150px;
}
#div9 {
margin: 283px 333px;
}
#div10 {
margin: 283px 599px;
}
#div11 {
margin: 279px 866px;
}
#div12 {
margin: 279px 1129px;
}
#div13 {
margin: 581px 312px;
}
#div14 {
margin: 581px 579px;
}
#div15 {
margin: 576px 858px;
}
#div16 {
margin: 577px 1129px;
}
#drag3 {
transform: translateY(-54%);
}

<!-- language: lang-html -->

&lt;div class=&quot;dropbox2&quot; id=&quot;div9&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div10&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div11&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div12&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div13&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div14&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div15&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox2&quot; id=&quot;div16&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div1&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div2&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div3&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div4&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div5&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div6&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div7&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;dropbox&quot; id=&quot;div8&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot;&gt;&lt;/div&gt;
&lt;img class=&quot;sails&quot; id=&quot;drag1&quot; src=&quot;https://drive.google.com/uc?export=view&amp;id=1ARGJDwdV9kH0IpOOL5hlm3lmOzOn81F1&quot; draggable=&quot;true&quot; ondragstart=&quot;drag(event)&quot; width=&quot;215&quot; height=&quot;210&quot;&gt;
&lt;img class=&quot;sails&quot; id=&quot;drag2&quot; src=&quot;https://drive.google.com/uc?export=view&amp;id=1ARGJDwdV9kH0IpOOL5hlm3lmOzOn81F1&quot; draggable=&quot;true&quot; ondragstart=&quot;drag(event)&quot; width=&quot;215&quot; height=&quot;210&quot;&gt;
&lt;img id=&quot;drag3&quot; src=&quot;https://drive.google.com/uc?export=view&amp;id=1vZIEa2VkNaQLdb8zEWSkKaEeSLOOFBFV&quot; draggable=&quot;true&quot; ondragstart=&quot;drag(event)&quot; width=&quot;290&quot; height=&quot;170&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 23:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定