Find parent and first child of it's parent

末鹿安然 提交于 2019-12-04 01:52:46

If you go up two levels, then find the next child, you're only going down one level -- you're setting the selected class on the <li>, not the <a> below it.. You need to go down another level. So it should be:

$('.selected').parent().parent().children(':first-child').children().addClass('selected');

Another way to get there is:

$('.selected').parent().siblings(':first-child').children().addClass('selected');
$('.selected').parent().siblings(':first-child').children().addClass('selected');

FIDDLE DEMO

$('.selected').closest('ul').find('li:first').addClass('selected');

reference closest

There is no need of total traversing back to the parent one after the other in a hierarchy, you can just do use parents() instead of parent() in this way

$('.selected').parents('ul.lowerMenu').children(:first-child').addClass('selected');

Try it this is working!!

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