简体   繁体   English

如何从元素js中获取所有孩子

[英]how to get all children from element js

how to get all chilredn from elemnt on javascript pure:如何从 javascript 上的元素中获取所有孩子:

i have this code:我有这个代码:

  const selectFaturamento = document.querySelector("div#faturamento");
  const optionsContainer = document.querySelector(".options-container");
  const optionsList = document.querySelectorAll(".option");

I basically need to get all the elements: .options-container.option我基本上需要获取所有元素: .options-container.option

but they are children of my element: select但他们是我元素的孩子:select

why do I have other elements that are not children that have the same class为什么我的其他元素不是具有相同 class 的子元素

When using document.querySelector or document.querySelectorAll , it uses the document as the parent.当使用document.querySelectordocument.querySelectorAll时,它使用document作为父级。 If you are looking for children of a specific selector, you can use that one instead of document to search inside of that node.如果您正在寻找特定选择器的子节点,则可以使用该选择器而不是document在该节点内进行搜索。 For example:例如:

  const selectFaturamento = document.querySelector("div#faturamento");
  const optionsContainer = selectFaturamento.querySelector(".options-container");
  const optionsList = optionsContainer.querySelectorAll(".option");

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

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