Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

javascript - how is it when I use querySelectorAll, that I am getting undefined?

    var initialCircle;
    var i;
    var randomXPos;
    var randomYPos;
    var colorArray;
    var interval01;
    var circleNodeList;
    colorArray=[
        'aliceblue','lavender','powderblue','lightblue','lightskyblue','skyblue','deepskyblue','lightsteelblue',    'dodgerblue','cornflowerblue','steelblue','cadetblue','mediumslateblue','slateblue','darkslateblue','royalblue','blue','mediumblue','darkblue','navy','midnightblue','blueviolet',  'indigo'];
    var randomColor;




    function startBouncingHoverCircles(){interval01 = setInterval(function(){randomXPos = Math.floor(Math.random() * 450) + 1;randomYPos = Math.floor(Math.random() * 300) + 1;randomColor=colorArray[Math.floor(Math.random() * colorArray.length)];initialCircle = document.createElement('div');document.querySelector("#container").appendChild(initialCircle);initialCircle.className="aces";initialCircle.style='height:30px;width:30px;border-;box-sizing:border-box;position:absolute;border:3px solid green;background-color:'+randomColor;initialCircle.style.top=randomYPos+'px';initialCircle.style.left=randomXPos+'px';},1);setTimeout(function(){clearInterval(interval01);colorChange()},200)}
    
function colorChange(){circleNodeList = document.querySelectorAll(".circles");console.log(circleNodeList.length);for (let i = 0; i < circleNodeList.length; i++) {
  circleNodeList[i].style.backgroundColor='red';
}}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.6.1/randomColor.min.js" integrity="sha512-vPeZ7JCboHcfpqSx5ZD+/jpEhS4JpXxfz9orSvAPPj0EKUVShU2tgy7XkU+oujBJKnWmu4hU7r9MMQNWPfXsYw==" crossorigin="anonymous"></script>
    <style>#container {
  width: 450px;
  height: 300px;
  position: relative;
  border: 1px solid black;
  margin: auto;
}</style>
</head>

<body>
    <button onClick="startBouncingHoverCircles()">Start</button>
<div id="container"></div>
    
    <script src="../Js/bouncingHoverCircles.js"></script>
</body>
</html>
question from:https://stackoverflow.com/questions/65941035/how-is-it-when-i-use-queryselectorall-that-i-am-getting-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

getElementbyclass name returns the Object of circles and then you have to loop through the object of cirlces and then apply style on it


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...