[英]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.querySelector
或document.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.