jQuery实现判断元素是否存在子元素功能

jQuery实现判断元素是否存在子元素功能

发布:admin2024-03-14 12:23分类:web前端标签:jQuery实现判断元素是否存在子元素功能

<!DOCTYPE html>
<html>
<head>
  <title>判断元素是否存在子元素</title>
  <script src=https://www.php.cn/faq/"https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

<div id="container">
  <p>这是一个包含子元素的容器</p>
</div>

<div id="empty-container">
  <!-- 这是一个空的容器 -->
</div>

<script>
  // 判断container元素是否包含子元素
  if($('#container').children().length > 0) {
    console.log('container元素包含子元素');
  } else {
    console.log('container元素不包含子元素');
  }

  // 判断empty-container元素是否包含子元素
  if($('#empty-container').children().length > 0) {
    jQuery实现判断元素是否存在子元素功能 console.log('empty-container元素包含子元素');
  } else {
    console.log('empty-container元素不包含子元素');
  }
</script>

</body>
</html>
登录后复制
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

欢迎使用手机扫描访问本站,还可以关注微信哦~