Javascript 如何使用jQuery从多个嵌套列表中更改并发列表项
如果我有以下嵌套列表Javascript 如何使用jQuery从多个嵌套列表中更改并发列表项,javascript,jquery,Javascript,Jquery,如果我有以下嵌套列表 <ul> <li class="hide">list 1.1</li> <li>list 1.2</li> <li>list 1.3 <ul> <li>list 2.1</li> <li class="hide">list 2.2</li> <li>list 2.3
<ul>
<li class="hide">list 1.1</li>
<li>list 1.2</li>
<li>list 1.3
<ul>
<li>list 2.1</li>
<li class="hide">list 2.2</li>
<li>list 2.3
<ul>
<li id="list-item">list 3.1</li>
<li class="hide">list 3.2</li>
</ul>
</li>
<li class="hide">list 2.4</li>
<li class="hide">list 2.5</li>
</ul>
</li>
<li class="hide">list 1.4</li>
</ul>
如何使其从所有li元素中选择,而不仅仅是当前ul中的元素
编辑添加:以下是一个关于JS Bin的示例:
我已经开发了一个详细的解决方案。如果有人有更好的解决方案,那么我想看看
var found = false;
$.each($("li"), function(i, li){
var $li = $(li);
if($li.attr("id") === "list-item"){
found = true;
return true; // Continue the .each
}
if(!found)
return true; // Continue the .each
if(!$li.hasClass('hide'))
return false; // Exit the $.each
$li.removeClass('hide');
return true; // Continue the .each
});
我已经开发了一个详细的解决方案。如果有人有更好的解决方案,那么我想看看
var found = false;
$.each($("li"), function(i, li){
var $li = $(li);
if($li.attr("id") === "list-item"){
found = true;
return true; // Continue the .each
}
if(!found)
return true; // Continue the .each
if(!$li.hasClass('hide'))
return false; // Exit the $.each
$li.removeClass('hide');
return true; // Continue the .each
});