简体   繁体   English

如何使用 jQuery 检测另一个 div 上的 div?

[英]How to detect a div over another div with jQuery?

How to detect a div over another div with jQuery?如何使用 jQuery 检测另一个 div 上的 div? any solution??任何解决方案?? thanks for you answer谢谢你的回答

To get this you can use the following code, if your one DIV is called i1 and the other div is called example Image1 - then you can get the offsetTop and offsetLeft positions and compare them.为此,您可以使用以下代码,如果您的一个 DIV 称为 i1 而另一个 div 称为 example Image1 - 那么您可以获得 offsetTop 和 offsetLeft 位置并进行比较。

    var y = document.getElementById('i1').offsetTop;
    var x = document.getElementById('i1').offsetLeft;


    var elementy = document.getElementById('image1').offsetTop;
    var elementx = document.getElementById('image1').offsetLeft;

    if (elementy == y) {
        if (elementx == x) {
            gameover();
        }
    }

You can use the children selector.您可以使用选择器。 Note it travels only a single level down the dom for anything else use find请注意,它仅在 dom 中向下移动一个级别以用于其他任何使用find

<div class="abc">

  <div class="def">Div 2</div>
  <div class="def">Div 3</div>

</div>

Somehow find the parent div.. using a class or id selector or choosing it smarlty by using first.以某种方式找到父 div .. 使用 class 或 id 选择器或通过首先使用来选择它。 last etc.最后等

$(".abc").children().each(function(){
$(this).html("test");
});

or use a selector或使用选择器

$(".abc").children(":first").append("test2");

http://jsfiddle.net/fVQB8/ Does that help you? http://jsfiddle.net/fVQB8/这对你有帮助吗?

You can try this or use $('#div').on('dragstart',function(evt)您可以尝试这个或使用$('#div').on('dragstart',function(evt)

var CardOnePosistion;
var MovedPosition;
        
$(function(){     
MovedPosition = $('#Moved').position();
CardOne = $('#CardOne').position();
 if (MovedPosition.left === CardOne.left && MovedPosition.top === CardOne.top) {
       alert("Div over another Div");
  } 
}

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

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