简体   繁体   English

如何在 JavaScript 中检查 div 是否包含图像元素? (不像背景图片,像包含的元素)

[英]How can I check to see if a div contains an image element in JavaScript? (Not like a background image, like the element contained)

I have a fairly simple program which moves a picture of a house between 6 different tiles.我有一个相当简单的程序,可以在 6 个不同的瓷砖之间移动房屋的图片。 The goal here is as follows:这里的目标如下:

  • When I drag a house on to an empty grass cell, I want the house to move to that cell.当我将房子拖到一个空的草地牢房时,我希望房子移动到那个牢房。 (This is already working.) (这已经在起作用了。)
  • When I drag a house onto a cell containing a house, I want something special to happen.当我将房屋拖到包含房屋的单元格上时,我希望发生一些特别的事情。 (Call a function) (调用函数)

Here's my code:这是我的代码:

 function allowDrop(ev) { ev.preventDefault(); } 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)); } function yeet() { alert("ekfui"); }
 .cell { border-radius: 10px; width: 100px; height: 100px; margin: 5px; padding: 5px; background-image: url("https://us.123rf.com/450wm/tuulijumala/tuulijumala1602/tuulijumala160200015/52473977-seamless-square-green-grass-texture-.jpg?ver=6"); }
 <!--What you actually see--> <div align="center"> <table> <tr> <td> <div id="gh" class="cell" ondrop="drop(event)" ondragover="allowDrop(event)"> <img src="http://worldartsme.com/images/free-house-clipart-1.jpg" draggable="true" ondragstart="drag(event)" id="drag1" width="100px" height="100px"> </div> </td> <td> <div class="cell" ondrop="drop(event); yeet();" ondragover="allowDrop(event)"></div> </td> <td> <div class="cell" ondrop="drop(event)" ondragover="allowDrop(event)"></div> </td> </tr> <tr> <td> <div class="cell" ondrop="drop(event)" ondragover="allowDrop(event)"></div> </td> <td> <div class="cell" ondrop="drop(event)" ondragover="allowDrop(event)"> <img src="http://worldartsme.com/images/free-house-clipart-1.jpg" draggable="true" ondragstart="drag(event)" id="drag2" width="100px" height="100px"> </div> </td> <td> <div class="cell" ondrop="drop(event)" ondragover="allowDrop(event)"></div> </td> </tr> </table> </div>

No jQuery or libraries please请不要使用 jQuery 或库

In your drop function, check if event currentTarget contains any img tags:在 drop 函数中,检查事件currentTarget包含任何img标签:

if (ev.currentTarget.querySelectorAll('img').length > 0) {
  alert('There\'s already a house there.');
} else {
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}

https://jsfiddle.net/xmdkj0y4/ https://jsfiddle.net/xmdkj0y4/

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

相关问题 Javascript - 如何检查元素是否包含图像? - Javascript - How can I check if an element contains an image? 如何缩放像图像一样的元素? - How can I scale an element like an image? 如何使用JavaScript将一个图像元素设置为另一个div的背景图像 - How can I set one image element as background image of another div using javascript 如何检查元素是否包含图像? - How to check if an element contains an image? 如何将 Javascript 元素放入 CSS 背景图像 - How can I put Javascript element into CSS background-image 如何检查div包含哪个背景图像? - How to check which background image div contains? DIV如何充当Javascript中的另一个元素? - How can a DIV act like another element in Javascript? 如何使用JavaScript查找被单击的元素(或具有背景图像的最近的子元素或父元素)的背景图像? - How can I find the background image of a clicked element (or the nearest child or parent element that has a background image) using JavaScript? 在jQuery中,如何在不使用JavaScript对图像的URL进行硬编码的情况下设置元素的背景图像? - In jquery, how can I set the background-image of an element without hardcoding the url of the image in my javascript? 如果DOM元素包含类,我如何检入JavaScript? - How can I check in JavaScript if a DOM element contains a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM