繁体   English   中英

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

[英]how to get all children from element js

如何从 javascript 上的元素中获取所有孩子:

我有这个代码:

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

我基本上需要获取所有元素: .options-container.option

但他们是我元素的孩子:select

为什么我的其他元素不是具有相同 class 的子元素

当使用document.querySelectordocument.querySelectorAll时,它使用document作为父级。 如果您正在寻找特定选择器的子节点,则可以使用该选择器而不是document在该节点内进行搜索。 例如:

  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