current position:Home>JavaScript gets and modifies elements. What's wrong with not extracting the classlist attribute

JavaScript gets and modifies elements. What's wrong with not extracting the classlist attribute

2022-04-29 06:29:44CSDN Q & A

The phenomenon and background of the problem
Operation results and error reporting contents
My solution ideas and tried methods
What I want to achieve
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <style> .main .listOne li { background-color: bisque; } .main .spanList span { background-color: rgb(150, 232, 62); } .main .divList div { background-color: rgb(38, 58, 209); } </style></head><body>    <div class="main">        <ol class="listOne">            <li>oneli</li>            <li>oneli</li>            <li>oneli</li>            <li>oneli</li>            <li>oneli</li>        </ol>        <div class="spanList">            <span>spanOne</span><br>            <span>spanOne</span><br>            <span>spanOne</span><br>            <span>spanOne</span><br>            <span>spanOne</span><br>        </div>        <div class="divList">            <div>divOne</div>            <div>divOne</div>            <div>divOne</div>            <div>divOne</div>            <div>divOne</div>        </div>    </div>    <script> var liListOne = document.getElementsByClassName(".listOne"); var allEl = document.querySelectorAll('.main'); console.log(allEl); var arrLi = liListOne[0]; console.log(liListOne); var a = allEl.classList; console.log(a); </script></body></html>



Take the answer :

because document.querySelectorAll It returns an array , So we need to add an index .

var a = allEl[0].classList;

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/109/202204191836161652.html

Random recommended