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