jQuery: excluding root element of a list tree (ul)

别说谁变了你拦得住时间么 提交于 2019-12-11 02:36:27

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/13976407/jquery-excluding-root-element-of-a-list-tree-ul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!