简体   繁体   English

如何使用 if/else 语句编写 javascript 函数来检查是否<div>类包含一个<img>或者<img>班级

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

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").该页面旨在让用户将图像(我创建了两个类,一个称为“sails”,另一个称为“boats”)到一个 div(名为“dropbox”和“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.我遇到的问题是,如果用户将图像放入已经包含图像的可放置 div 中,其中一个图像会消失并且无法恢复。

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/else 语句创建一个函数来检查 div 是否包含图像。 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.如果是这样,那么我希望拖放功能保持其默认设置,这样它就不会落入 div 并从用户那里消失。 Else, it executes the code I already have that turns the default nature off so that it can drop into the div.否则,它会执行我已经拥有的代码,将默认性质关闭,以便它可以放入 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?我怎样才能说出条件,以便我可以检查 div 是否包含图像,以及如果条件为真,我怎样才能用代码块来不允许图像放入该 div?

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 的链接:

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

Any help would be greatly appreciated :)任何帮助将不胜感激 :)

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.这里的第一个问题是ev.target.appendChild将在第一次放置时将图像作为子项添加到div ,并在第二次放置时将其附加到img标签,从而在每个后续放置中创建更深的嵌套关系。 This is observable by console.log(ev.target) in that function.这可以通过该函数中的console.log(ev.target)观察到。

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.因此,一种方法是首先确保我们要附加到的节点是 div,然后我们的条件将检查该 div 是否已经有任何子节点。 I also simplified your event listeners.我还简化了您的事件侦听器。 Here is an example:下面是一个例子:

 // When the draggable p element enters the droptarget, change the DIVS's border style document.addEventListener("dragenter", function(event) { if (event.target.className == "dropbox" || event.target.className == "dropbox2") { event.target.style.border = "3px dotted red"; } }); // When the draggable p element leaves the droptarget, reset the DIVS's border style 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">

You need to write your condition in the drop function itself.您需要在drop函数本身中写入您的条件。

Here's how you might do it:您可以这样做:

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");

    // check if the target container has an image already
    var targetHasImage = ev.target.querySelector('img');

    // check if the target container is not an image
    var targetIsImage = ev.target.tagName === 'IMG'

    // add the image if the target does not have an image and is not an image itself
    if (!targetHasImage && !targetIsImage) {
        ev.target.appendChild(document.getElementById(data));
    }
}

At drop event, attach a new class : noDrop ev.target.classList.add("noDrop");在 drop 事件中,附加一个新class : noDrop ev.target.classList.add("noDrop"); to the target div when the first element is dropped, in order to "mark" him as occupied.当第一个元素被删除时,到目标 div,以便“标记”他被占用。

At the second drop ask the parent if it has the class noDrop, if it has, do nothing, otherwise attach the new object.在第二次放置时,询问父对象是否有class noDrop,如果有,则什么都不做,否则附加新对象。

 // When the draggable p element enters the droptarget, change the DIVS's border style document.addEventListener("dragenter", function(event) { if (event.target.className == "dropbox") { event.target.style.border = "3px dotted red"; } }); document.addEventListener("dragenter", function(event) { if (event.target.className == "dropbox2") { event.target.style.border = "3px dotted red"; } }); // When the draggable p element leaves the droptarget, reset the DIVS's border style document.addEventListener("dragleave", function(event) { if (event.target.className == "dropbox") { event.target.style.border = ""; } }); document.addEventListener("dragleave", function(event) { if (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) { debugger var parentID = ev.target.parentNode.className var data = ev.dataTransfer.getData("text"); if (parentID.includes("noDrop")) { console.log("no transfer"); ev.preventDefault(); } else { ev.preventDefault(); ev.target.appendChild(document.getElementById(data)); ev.target.classList.add("noDrop"); } }
 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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM