问题
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