检查 div 是否包含 jQuery 中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/892329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:03:27  来源:igfitidea点击:

check if div contains an element in jQuery

jqueryhtmldrag-and-drop

提问by Tom

I'm bulding some drag and drop widgets in jQuery, once they've been dropped I need to check if my drag and droppable widget are inside another div.

我正在 jQuery 中构建一些拖放小部件,一旦它们被放下,我需要检查我的可拖放小部件是否在另一个 div 内。

<div id="droptarget">
    <div class="widget">I'm a widget!</div>
</div>

I've had a look at $('#droptarget').eachbut can't seem to figure it out. Any ideas?

我看过,$('#droptarget').each但似乎无法弄清楚。有任何想法吗?

回答by cletus

If you want to select the outer div:

如果要选择外部 div:

$("#droptarget:has(div.widget)")

If you want to select the widget:

如果要选择小部件:

$("#droptarget > div.widget")

回答by Ilya Birman

I would start with

我会开始

if ($ ('#droptarget .widget')) {
  // do something
}