jQuery:排除列表树的根元素(ul) [英] jQuery: excluding root element of a list tree (ul)

查看:61
本文介绍了jQuery:排除列表树的根元素(ul)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用来自堆栈溢出的一段jquery代码.这是页面的链接:

https://stackoverflow.com/questions/2...76196#13976196

除了根元素之外,我需要隐藏一个大型的UL树形结构.上面的代码非常有用,除了我无法理解以下行:

代码:

$('ul, li', $('#lesson-sidebar ul li')).hide();

该代码隐藏了列表中的所有UL和LI项目,但排除了根元素.我不明白如何将$('#lesson-sidebar ul li')添加为选择器参数,将其从hide命令中排除.

我知道hide命令不会切换可见状态,所以这并不像他们隐藏根元素并立即显示它一样.大多数其他描述都依赖于诸如:not排除根元素之类的命令.

谁能解释为什么该命令没有隐藏根元素?请注意,如果删除$('#lesson-sidebar ul li')部分,则实际上隐藏了根元素.

解决方案

$('ul, li', $('#lesson-sidebar ul li'))表示

选择所有作为$('#lesson-sidebar ul li')

子级的ulli

这是该选择器的模式$('children selector', parent) 如您在示例中看到的,parent不是字符串,而是DOM或jQuery元素

I've been using a piece of jquery code from stack overflow. Here's the link to the page:

https://stackoverflow.com/questions/2...76196#13976196

I need to hide a large UL tree structure except the root elements. The code above works great except I can't understand the following line:

Code:

$('ul, li', $('#lesson-sidebar ul li')).hide();

That code hides all of the UL and LI items in the lists, but excludes the root elements. I don't understand how adding $('#lesson-sidebar ul li') as a selecond selector parameter excludes it from the hide command.

I know the hide command doesn't toggle the visible state so it's not like they're hiding the root element and showing it right away. Most other descriptions rely on commands such as :not to exclude the root element.

Can anyone explain why the root elements are not being hidden with that command? Note that if I remove the $('#lesson-sidebar ul li') part the root elements are in fact hidden.

解决方案

That $('ul, li', $('#lesson-sidebar ul li')) means

Select all ul and li that are children to $('#lesson-sidebar ul li')

Here's the pattern of that selector $('children selector', parent) As you can see in the example parent is not string it is a DOM or jQuery element

这篇关于jQuery:排除列表树的根元素(ul)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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