gpt4 book ai didi

javascript - 当我尝试以编程方式调整时,为什么 div 和 span 为空值?

转载 作者:行者123 更新时间:2023-11-28 03:02:55 25 4
gpt4 key购买 nike

因此,我习惯于使用 Ruby 和 Java 来设计逻辑样式,但我对前端和 Javascript 不熟悉。当我尝试 getElementByIdgetElementsByClassName 时,我遇到了 div 和 span 未定义或 null 的问题。当我不应该只调整背景或添加 div 时,我不得不偷工减料。

最新一期如下:

var stars = document.getElementById("starContainer");

function starz() {
for ( let i = 1; i <= 60; i++){
var newDiv = document.createElement('div');
newDiv.style.position = "absolute";
newDiv.className = "star";
newDiv.style.height = "15px";
newDiv.style.width = "15px";
newDiv.style.color = "white";
newDiv.innerHTML = "star";
newDiv.style.borderRadius = "15px";
newDiv.style.backgroundColor = "white";
newDiv.style.background = "radial-gradient(circle, rgba(255,255,255,1.0) 1%, rgba(255,255,255,0.0))";
newDiv.style.left = setLeftPosition();
newDiv.style.top = setTopPosition();

stars.appendChild(newDiv);
}
}

function setLeftPosition(){
let min = Math.ceil(0);
let max = Math.floor(1400);
let rand = Math.floor(Math.random() * (max - min)) + min;

return rand + "px";
}

function setTopPosition(){
let min = Math.ceil(0);
let max = Math.floor(490);
let rand = Math.floor(Math.random() * (max - min)) + min;

return rand + "px";
}
#scene {
position: relative;
overflow: hidden;
height: 1000px;
width: 100%;
background-color:rgb(0, 24, 63);
background: linear-gradient(35deg,rgb(0, 24, 63) 10%, rgb(17, 0, 14));
}


#starContainer {
color: white;
z-index: -1;
height: inherit;
width: inherit;
}

#ground {
position: absolute;
height: 245px;
width: 650px;
border-left:20px solid transparent;
margin-top: 800px;
margin-left: 68%;
background-color: rgb(2, 2, 2);
border-radius: 50px;
}

#middle-ground {
z-index: 2;
position:absolute;
border-bottom: 175px solid rgb(2, 2, 2);
border-left: 150px solid transparent;
border-top: 20px solid transparent;
margin-top:792px;
margin-left: 845px;
}


#slanted-border {
position:absolute;
width: 200px;
border-bottom: 115px solid rgb(5, 5, 5);
border-left: 150px solid transparent;
border-top: 50px solid transparent;
margin-top:845px;
margin-left: 730px;

}
<!DOCTYPE html>
<html>
<head>


<!-- title & links to scripts & css--->


<div id="scene" onload="starz()">
<span id="starContainer">
<!-- <p> I AM JESUS I AM JESUS</p> -->
</span>
<div id="slanted-border"></div>
<div id="middle-ground"></div>
<div id="lower-ground"></div>
<div id="ground"></div>
</div>


</body>
</html>

所以这行不通。记录“stars”(包含 starContainer 的变量)返回 null。我不明白怎么了。其他时候,我有完全样式化的 div,我必须通过类名一个一个地获取并循环遍历它们导致 undefined 并且样式化的对象没有值。

但是,有时它会起作用!在后一期中,我强行使用它并通过 ID 抓取了每个 div,并且代码有效。在这一个上,它可以在 codepen 上运行(如果我将它设置为 onclick,而不是 onload),并且可以在我用来上传该代码片段的提供的 fiddle 中运行,但它不能作为 onload、onfocus 或 onclick 运行在实际的浏览器中。

请指教

最佳答案

尝试将 var stars = document.getElementById("starContainer"); 移动到 starz 函数中,以便它在文档加载时初始化:

function starz() {
var stars = document.getElementById("starContainer");
for ( let i = 1; i <= 60; i++){
var newDiv = document.createElement('div');
newDiv.style.position = "absolute";
newDiv.className = "star";
newDiv.style.height = "15px";
newDiv.style.width = "15px";
newDiv.style.color = "white";
newDiv.innerHTML = "star";
newDiv.style.borderRadius = "15px";
newDiv.style.backgroundColor = "white";
newDiv.style.background = "radial-gradient(circle, rgba(255,255,255,1.0) 1%, rgba(255,255,255,0.0))";
newDiv.style.left = setLeftPosition();
newDiv.style.top = setTopPosition();

stars.appendChild(newDiv);
}
}

function setLeftPosition(){
let min = Math.ceil(0);
let max = Math.floor(1400);
let rand = Math.floor(Math.random() * (max - min)) + min;

return rand + "px";
}

function setTopPosition(){
let min = Math.ceil(0);
let max = Math.floor(490);
let rand = Math.floor(Math.random() * (max - min)) + min;

return rand + "px";
}

关于javascript - 当我尝试以编程方式调整时,为什么 div 和 span 为空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46104247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com