查找父级和父级的第一个子级 [英] Find parent and first child of it's parent

查看:180
本文介绍了查找父级和父级的第一个子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个元素的父级,它是父级第一个孩子,代码如下:

I am trying to find the parent of an element and it's parent first child, the code is like this:

        <ul class="lowerMenu">
            <li><a href="" class="selected">Welcome</a></li>
            <li><a href="">Logo</a></li>
            <li><a href="">Websites</a></li>
            <li><a href="">Stationery</a></li>
            <li><a href="">Illustration</a></li>
            <li><a href="">Full Pack</a></li>
        </ul>

function setIntervalAndExecute() {
    changeImages();
    setTimeout(function(){
        showImages(imagesArray);
    },500);
    var intervalot = window.setInterval(function(){
        changeImages();
        var selected = $('.selected');
        if (!selected.parent().is(':last-child')) {
            $('.selected').parent().next().children().addClass('selected');
            selected.removeClass('selected');
        } else {
            $('.selected').parent().parent().children(':first-child').addClass('selected');
            selected.removeClass('selected'); // nesho ne mi rabotit ovdeki
        }
        window.setTimeout(function(){
            showImages(imagesArray);
        },500);
    },10000);
    return intervalot;
}
var intervalot = setIntervalAndExecute();

我知道这有点复杂,但我是新的jquery, do是在类'selected'获取到我想要删除它的最后一个元素,并将其设置为第一个元素。我尝试了这个,但它似乎不工作

I know it's a little complicated but i'm new to jquery , so what i'm trying to do is after the class 'selected' gets to the last element i want to remove it and set it to the first element. I've tried with this but it doesn't seem to be working

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

,当它到达最后一个元素时,间隔执行两次,然后停止。
这是我正在处理的网站:

and when it gets to the last element the interval is executing twice and then stops. This is the site that i'm working on:


http://nikodola.com/testsite


推荐答案

你上去两个级别,然后找到下一个孩子,你只有一个级别 - 你在上设置选择 < li> ,而不是它下面的< a> 。你需要下一个级别。所以它应该是:

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');

另一种方法是:

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

这篇关于查找父级和父级的第一个子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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