gpt4 book ai didi

javascript - 查询;检查对象是否有子 $(this)

转载 作者:技术小花猫 更新时间:2023-10-29 12:19:09 27 4
gpt4 key购买 nike

我想知道如何以最好的方式查看容器 div 是否包含子元素。我有一个在 div id=unread 或 div id=read 的子元素上触发的点击事件。我想看看这个 child 在哪里。

我的想法是这样的:

if ($("#unread").find($(this)))
alert("unread");
else
alert("read");

编辑:$(this) 是来自#unread 或#read 的两个级别的后代。

问候

最佳答案

利用:.children()

if( $("#unread").children().length > 0)
alert("unread");
else
alert("read");

编辑

if($(event.target).closest('#unread').length > 0)
alert('unread');
else
alert('read');

关于javascript - 查询;检查对象是否有子 $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6530739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com