简体   繁体   English

列出所有子元素的类名(Vanilla Javascript)

[英]List the class names of all the child elements (Vanilla Javascript)

I am having the following structure: 我具有以下结构:

<div class="outter">
  <div class="parent">
     <div class="child is-visible">1</div>
     <div class="child is-visible">2</div>
     <div class="child is-visible">3</div>
     <div class="child is-visible">4</div>
  </div>
</div>

and trying to get inside the parent div and output a console.log("Elements exist") if div with classname "child" contains is-visible . 并尝试进入父div并输出console.log("Elements exist")如果类名称为"child" div包含is-visible

I have managed to get inside the .parent div but not sure how to get inside the child elements. 我设法进入.parent div内,但不确定如何进入子元素。

The JS code I have used is: 我使用的JS代码是:

var n = document.getelementById("outter");

if(n.classList.contains("parent")){
   var m = n.getAttribute("class");
   console.log(m);
}

I know there are quite a lot of conversations and tutorials but it seems that I can't find one that resolves my problem. 我知道有很多对话和教程,但是似乎找不到解决我的问题的对话和教程。 Can you please help? 你能帮忙吗?

n refers to the outter DIV (except you should be looking it up by class, not ID). n表示outter DIV(除非您应该按类而不是ID进行查找)。 The class names you're looking for are on elements inside it, so accessing n 's class list won't work for this. 您要查找的类名称位于其中的元素上,因此无法访问n的类列表。

You can use a selector: 您可以使用选择器:

n = document.querySelector("#outter");
if (n.querySelector(".parent .child.is-visible")) {
    console.log("Elements exist");
}

You can use Element.childNodes to get all of an element's children. 您可以使用Element.childNodes来获取元素的所有子元素。

 <div class="outter"> <div class="parent"> <div class="child is-visible">1</div> <div class="child is-visible">2</div> <div class="child is-visible">3</div> <div class="child is-visible">4</div> </div> </div> <script> var parent = document.getElementsByClassName("parent")[0]; var children = parent.childNodes; for(let i = 0; i < children.length; i++){ var classname = children[i].className; if(classname&&classname.includes("child is-visible")){ console.log("Class name contains child and is-visible"); } children[i].innerHTML += " <b>Child</b>"; } </script> 

Use the function querySelectorAll with this selector div.outter div.parent .child.is-visible , this way you're selecting only those children within div .parent which is within of div.outter 使用功能querySelectorAll此选择div.outter div.parent .child.is-visible ,这样你只选择内那些孩子div .parent这是内div.outter

Document.querySelectorAll()

The Element method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors. Element方法querySelectorAll()返回一个静态(非实时)NodeList,它表示与指定选择器组匹配的文档元素的列表。

Look at this code snippet with DIVs .is-visible grouped by different DIVs, this is to show how it works the selector div.outter div.parent .child.is-visible 看看这个由.is-visible按不同DIVs分组的代码片段,这是为了展示选择器div.outter div.parent .child.is-visible是如何工作的

 var children = document.querySelectorAll("div.outter div.parent .child.is-visible"); console.log(children.length) // prints 4 and not 8 which is OK. 
 <div class="outter"> <div class="parent"> <div class="child is-visible">1</div> <div class="child is-visible">2</div> <div class="child is-visible">3</div> <div class="child is-visible">4</div> </div> </div> <div class="myanother-outter"> <div class="parent"> <div class="child is-visible">1</div> <div class="child is-visible">2</div> <div class="child is-visible">3</div> <div class="child is-visible">4</div> </div> </div> 

If you need to check the first div with class .outter then use the function document.querySelector() , this function will return the first div regardless of the amount of div with class .outter . 如果您需要检查.outter类的第一个div,然后使用函数document.querySelector() ,则此函数将返回第一个div,而与class .outter的div的数量.outter

This approach checks for the result of querySelector because if the DOM doesn't have any DIV element with class .outter the result will be null. 这种方法检查querySelector的结果,因为如果DOM没有任何带有.outter类的DIV元素,则结果将为null。

document.querySelector()

The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. Document方法querySelector()返回文档中与指定选择器或选择器组匹配的第一个Element。 If no matches are found, null is returned. 如果找不到匹配项,则返回null。

var children = firstOutter ? firstOutter.querySelectorAll("div.parent .child.is-visible") : []

 var firstOutter = document.querySelector('div.outter:nth-child(1)'); var children = firstOutter ? firstOutter.querySelectorAll("div.parent .child.is-visible") : []; console.log(children.length) // prints 2 and not 6 which is OK. 
 <div class="outter"> <div class="parent"> <div class="child is-visible">1</div> <div class="child is-visible">2</div> </div> </div> <div class="outter"> <div class="parent"> <div class="child is-visible">1</div> <div class="child is-visible">2</div> <div class="child is-visible">3</div> <div class="child is-visible">4</div> </div> </div> 

暂无
暂无

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

相关问题 在 Vanilla JavaScript 中过滤掉父元素中的所有子元素 - Filter out all child elements from parent in Vanilla JavaScript Vanilla JS - 可以在子元素上添加/删除类,但所有类都处于活动状态 - Vanilla JS - Can add/remove class on child elements but all are active 在原始JavaScript中删除带有特定子级(嵌套至少2个级别)的所有元素 - Remove all elements with a certain child (nested at least 2 levels in), in vanilla JavaScript 如何检查DOM中div的所有类名是否都有一个带有vanilla Javascript的特定字符串? - How to check if all class names of divs in the DOM have a specific string with vanilla Javascript? Plain Vanilla JavaScript - 重置所有元素的外观 - Plain Vanilla JavaScript - Reset appearance of all elements 使用 vanilla JavaScript 切换类列表的问题 - Problem with toggling class list with vanilla JavaScript 使用vanilla javascript将单个子元素插入/附加到多个父元素 - Insert/Append single child element to multiple parent elements with vanilla javascript 使用vanilla javascript添加匹配类名的增量数据属性 - Add incremental data attribute matching class names with vanilla javascript 使用香草 javascript 删除待办事项列表中的父项,包括子项 - delete parent item including child on a todo-list with vanilla javascript 如何 select 除了使用 Vanilla Javascript 悬停的元素之外的所有元素? - How to select all elements except the one hovered using Vanilla Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM