用jQuery获取叶节点的最有效方法是什么? [英] What is the most efficient way to get leaf nodes with jQuery?

查看:74
本文介绍了用jQuery获取叶节点的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从DOM中获取所有叶子节点(比如div)?如果没有简单而神奇的选择器,我会尝试寻找一种最佳解决方案.我考虑过使用"reachedElem.find('div').length == 0",但是我不确定这是否是正确的方向.有什么想法吗?

How can one get all the leaf nodes, let's say divs, from the DOM? I am trying to think of an optimal solution, if there's no easy and magical selector for this. I thought of using the "reachedElem.find('div').length == 0" but I am not sure if this is the right direction. Any ideas?

推荐答案

您可以使用以下选择器

$('div:not(:has(*))')

上面的选择器将选择所有没有子级的DIV.

Above selector will select all the DIVs who don't have any children.

这篇关于用jQuery获取叶节点的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆