javascript - 如何从 HTML 中提取 "parent with two child' s"模式

标签 javascript jquery html

我需要从 HTML 中提取所有有两个 child 的 parent

示例 1

<parent>
 <child>
  <tag></tag>
 </child>
 <child></child>
</parent>

示例 2

<parent>
 <parent_and_child>
  <tag></tag>
  <tag></tag>
 </parent_and_child>
 <child></child>
</parent>

示例 1 应输出 <parent>示例 2 应输出 <parent><parent_and_child>

最佳答案

jQuery(function($){
        $('.common').each(function(){
          //If tag has 2 children get the name of node
            if ($(this).children().length == 2 ) {
               console.log($(this)[0].nodeName);

            }
        });
})

		
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

<!-- Add the common class to each tag -->
<parent class = "common">
 <parent_and_child class = "common">
  <tag class = "common"></tag>
  <tag class = "common"></tag>
 </parent_and_child>
 <child class = "common"></child>
</parent>

关于javascript - 如何从 HTML 中提取 "parent with two child' s"模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56685853/

相关文章:

javascript - Cordova - 如何访问设备存储?

javascript - 如何从 JSON 中获取正确的值?

javascript - jQuery 选择器是快照还是实时更新

javascript - 使用 jQuery 设置 css 样式

javascript - 工具提示在生成的内容中不起作用

html - CSS。 div 的内容在 div 之外

javascript - 如何获取 Google 小工具 URL 参数

javascript - 显示 block 和无正在更改表格缩进

javascript - 向服务/产品添加百分比的正确方法是什么?

javascript - 使用 ng-show 和 $window.onload 在页面加载后显示元素