check if div contains an element in jQuery

随声附和 提交于 2020-01-13 09:09:10

问题


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.

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

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


回答1:


If you want to select the outer div:

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

If you want to select the widget:

$("#droptarget > div.widget")



回答2:


I would start with

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


来源:https://stackoverflow.com/questions/892329/check-if-div-contains-an-element-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!