简体   繁体   English

包装在不同UL中的下一个子项LI

[英]Next child LI that is wrapped in a different UL

I have the following list: 我有以下清单:

<ul>
    <li class="item">One</li>
    <li class="item">Two</li>
    <li class="item">Three
        <ul>
            <li class="item">Something Original</li>
            <li class="item selected">Something</li>
        </ul>
    </li>
    <li>Four 
        <ul>
            <li class="item">I want this selected next</li>
            <li class="item">Good</li>
        </ul>
    </li>
</ul>

Using jQuery, how do I find the next li with the class="item" since it is wrapped in a different container. 使用jQuery,我如何找到带有class="item"的下一个li ,因为它包装在另一个容器中。 Obviously I cannot do $(".selected").next(".item") so how else can I do it? 显然我无法执行$(".selected").next(".item")那么我还能怎么做?

JSFiddle: http://jsfiddle.net/q3f6v7zz/ JSFiddle: http : //jsfiddle.net/q3f6v7zz/

Since the li elements are nested and you know that you want the next appearing li with a particular class, you can use .index() and do something like this 由于li元素是嵌套的,并且您知道要使用特定的类来显示下一个li,因此可以使用.index()并执行类似的操作

var $li = $('.item'); // <--- get the list of all lis with class .item
var index = $li.index($('.selected')); // <--- find the index of the one with .selected amongst all the lis
console.log($li.eq(index+1).html()); // <--- index+1 because you need the next appearing li after selected

If you want to move the selected class on keydown something like this should do 如果要在按下按键时移动选定的类,则应执行以下操作

var $li = $('.item');
$(document).on('keydown', function(e) {
  if (e.keyCode == 40) {
    var index = $li.index($('.selected'));
    $li.eq(index).removeClass('selected');
    index = (index+1) % $li.length; // <--- to rotate the values from 0 to count of li.item elements
    $li.eq(index).addClass('selected');
  }
});

 var $li = $('.item'); $(document).on('keydown', function(e) { if (e.keyCode == 40) { var index = $li.index($('.selected')); $li.eq(index).removeClass('selected'); index = (index+1) % $li.length; $li.eq(index).addClass('selected'); } }); 
 .selected { background: green; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <ul> <li class="item">One</li> <li class="item">Two</li> <li>Three <ul> <li class="item">Something</li> <li class="item selected">Something Else</li> </ul> </li> <li>Four <ul> <li class="item">I want this selected next</li> <li class="item">Good</li> </ul> </li> </ul> 

Not sure what you are exactly looking for but you can use $(Element").parent().parent().find("li"); 不确定您要寻找的是什么,但是可以使用$(Element").parent().parent().find("li");

So in other words .parent() may be what you are looking for there is also .sibling() to find or you may want $('li').closest('ul').find('li') which will go up the tree to find the nearest ul to the one you are looking for https://api.jquery.com/closest/ 所以换句话说.parent()可能就是您正在寻找的东西,也有.sibling()可以找到,或者您可能想要$('li').closest('ul').find('li')上树找到与您要查找的ul最接近的ul https://api.jquery.com/closest/

You may also use: 您还可以使用:

Vanilla JS to do something similar to what was discussed by others with $index if it makes more sense to you: Again this isn't as efficient but that is basically what JQuery is doing: 如果对您更有意义,Vanilla JS会执行与其他人使用$ index讨论的内容类似的操作:再次,它效率不高,但基本上就是JQuery所做的:

var myLis = document.getElementsByTagName('li');
var wantedIndex;
for(var i = 0;i<myLis.length; i++){
   if(myLis[i].className === "active"){
      wantedIndex = i+1; //gets the li which is next when selecting all lis
       }
}

You can get the index of the selected element within all li s, and then increment that index to get the next one. 你可以得到所有内所选元素的索引li s,然后递增该索引,以获得下一个。

 $("ul").on("click", "li.item.selected", function() { var all_li = $("li.item"); var selected_index = all_li.index(this); var next_li = all_li.eq((selected_index + 1) % all_li.length); $(this).removeClass("selected"); next_li.addClass("selected"); }); 
 .item.selected { background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="item">One</li> <li class="item">Two</li> <li class="item">Three <ul> <li class="item selected">Something</li> </ul> </li> <li>Four <ul> <li class="item">I want this selected next</li> <li class="item">Good</li> </ul> </li> </ul> 

I used the modulus so it will wrap around at the end. 我使用了模数,因此它将在末尾环绕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM