jQuery可以在同一级别访问元素

时间:2013-06-14 16:02:58

标签: jquery

我有

<ul id="notifications">
   <li onclick="toggleArrived('{{Aircraft}}',this)"><div class="top notoff"><img src="../lib/img/arrived.png" /></div><div class="bottom notoff">Arrived</div></li>
   <li onclick="toggleBoard('{{Aircraft}}',this)"><div class="top notoff"><img src="../lib/img/board.png" /></div><div class="bottom notoff">Ok to Board</div></li>
   <li onclick=""><div class="top notoff"><img src="../lib/img/done.png" /></div><div class="bottom notoff">Done</div></li>
</ul>

现在点击第一个“li”,我也想改变最后一个“li”。

function toggleArrived(ac,obj) {
        for (var i=0;i<data.Aircrafts.length;i++) {
            if (data.Aircrafts[i].Aircraft==ac) {
                if (obj.childNodes[0].className=="top notoff") {
                    data.Aircrafts[i].Aircraft.Arrived="YES";
                    obj.childNodes[0].className="top noton";
                    obj.childNodes[1].className="bottom noton";

                ??? if ($("#notifications li:last-child.childNodes[0]").className =="top notoff") {
                        $("#notifications li:last-child.childNodes[0]").className = "top noton";
                        $("#notifications li:last-child.childNodes[1]").className = "bottom noton";
                    }
                } else {
                    data.Aircrafts[i].Aircraft.Arrived="NO";
                    obj.childNodes[0].className="top notoff";
                    obj.childNodes[1].className="bottom notoff";
                }
            }
        }
    }

我该怎么做?

1 个答案:

答案 0 :(得分:1)

在你的功能中:

var lis = obj.parent().children('li');  // Will give you all the lis

或者您可以使用:

var lis = obj.siblings().addBack();     // Will give you all the lis

然后:

lis.filter(':nth-child(x)');            // Will give you li # x

此外:

lis.filter(':last-child');              // Will give you the last li