简体   繁体   English

如何使用纯javascript选择嵌套的ul元素

[英]How to select nested ul element using pure javascript

For something seemingly simple, it's causing me some trouble.对于一些看似简单的事情,它给我带来了一些麻烦。

I have the following pattern:我有以下模式:

    <div className="row">
      <ul className="col-md-4 options-ul">

Question is how do i select the nested ul using plain javascript?问题是如何使用普通 javascript 选择嵌套的 ul? I've tried various renditions of querySelector and querySelectorAll to no avail.我已经尝试了 querySelector 和 querySelectorAll 的各种演绎,但无济于事。 tia蒂亚

You can use document.querySelector(".options-ul") after you switch your className attribute to class .您可以在将className属性切换为class后使用document.querySelector(".options-ul")

If, for some reason, you need to use classname (HTML attributes are lower case):如果出于某种原因,您需要使用classname (HTML 属性是小写的):

document.querySelector("[classname ~= 'options-ul']");

You would use:你会使用:

document.querySelectorAll(".row > ul")

to get the immediate ul child of every .row element.获取每个.row元素的直接ul子元素。 Use document.querySelector instead of querySelectorAll if you just want the first result.如果您只想要第一个结果,请使用document.querySelector而不是querySelectorAll

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

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