我需要从 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/