简体   繁体   English

如何选择 <li> 内 <ul>

[英]How to select <li> inside <ul>

Assume I have html structure as 假设我有html结构

ul
  li //this one
    ul
      li
      li
      li
  li //this one

I don't want to get all li 's. 我不想得到li的全部。 As I commented I just want to get first segment li's. 正如我评论的那样,我只想得到第一段李。 How can I select them? 我该如何选择它们?

document.querySelectorAll("ul > li");

returns all li 's. 返回所有li的。

edit: actually this is just a snippet of the tree. 编辑:实际上这只是树的一小部分。
I can't modify the structure as adding class or id. 我无法将结构修改为添加类或ID。 I'm looking for an answer to get the list of first layer li s 我在寻找的答案,得到第一层的列表li小号

You need to select all the li items where the grandparent is not another list item. 您需要选择祖父母不是另一个列表项的所有li项。

:not(li) > * > li {}

This will only work if you don't have extra elements in the markup. 这仅在标记中没有额外元素时才有效。

For example it would fail with: 例如,它将失败:

<ul>
  <li>
    <div>
      <ul>
        <li>

A slower, but more reliable, approach would be to get all the list items and then filter out ones which had list item ancestors. 一种较慢但更可靠的方法是获取所有列表项,然后筛选出具有列表项祖先的项。

 var lis = document.getElementsByTagName('li'); var lis_without_li_ancestors = []; for (var i = 0; i < lis.length; i++) { var element = lis[i]; if (!has_li_ancestor(element)) { lis_without_li_ancestors.push(element); } } console.log(lis_without_li_ancestors); function has_li_ancestor(element) { var parent = element.parentNode; if (parent.tagName.toLowerCase() === "body") { return false; } else if (parent.tagName.toLowerCase() === "li") { return true; } else { return has_li_ancestor(parent); } } 
 <ul> <li> <ul> <li>... </ul> </ul> 

In jQuery you can use this selector ul > li:not('ul > li > ul > li') : 在jQuery中你可以使用这个选择器ul > li:not('ul > li > ul > li')

 $("ul > li:not('ul > li > ul > li')").css("background", "green"); 
 ul > li { background: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> First <ul> <li> Second <ul> <li>Third</li> </ul> </li> <li>Second</li> <li>Second</li> <li>Second</li> </ul> </li> <li>First</li> <li>First</li> <li>First</li> </ul> 

Try this it should do the trick : 试试这个它应该做的伎俩:

$("ul > li").not("ul li ul li");

Also this should work in your case : 这也适用于你的情况:

$("ul").first().children("li");

You can add an id to that specific li : 您可以为该特定li添加id

<li id="someID"></li>

then select it using: 然后选择它:

var chosenOne = document.getElementById("someID");

You could traverse the first ul's children and look for li's like the following. 你可以遍历第一个ul的孩子,并像下面一样寻找li。

function firstLis() {
  var firstUl = document.querySelector('ul');
  var list = [];
  for(var i = 0; i < firstUl.children.length; i++) {
    var element = firstUl.children[i];
    if(element.tagName === 'LI')
      list.push(element);
  }
  return list;
}

use :scope 用途:scope

 console.log(source.querySelectorAll("ul:scope > li")); 
 <ul id="source"> <li> this <ul> <li>not this</li> <li>not this</li> <li>not this</li> </ul> </li> <li> this </li> </ul> 

Snapshot from Chrome console: 来自Chrome控制台的快照:

在此输入图像描述

You can get the father's direct children. 你可以得到父亲的直接孩子。 In this case, the "lis": 在这种情况下,“lis”:

 console.log(document.querySelector("ul").children) 
 <ul> <li> <ul> <li></li> <li></li> <li></li> </ul> </li> <li> <ul> <li></li> <li></li> <li></li> </ul> </li> </ul> 

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

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