gpt4 book ai didi

javascript - 在追加 child 之前添加CSS

转载 作者:行者123 更新时间:2023-11-30 12:50:39 24 4
gpt4 key购买 nike

所以我有一个 div(id 为“thecolor2”),我想附加到一个无序列表,但在我附加它之前,我想将它的背景颜色设置为一个具有十六进制值的变量代码。但是,出于某种原因,它不接受颜色。

这是 CSS:

#thecolor2{
height: 50px;
width: 50px;
border-radius: 100px;
border: 1px solid yellow;
position: relative;
bottom: 635px;
}

她是 HTML:

<ul id = "allposts"></ul>

这是 JS:

var thestream = document.getElementById('allposts');
var oneofpost = document.createElement('li');
var thecolor2 = document.createElement('div');
thecolor2.id = "thecolor2";
$("#thecolor2").css("background-color", color);
thestream.appendChild(oneofpost);
thestream.appendChild(thecolor2);

最佳答案

您不能使用 jQuery ID 选择器来匹配尚未添加到文档树中的节点。您可以简单地使用普通 DOM 来设置其内联 CSS 样式,如下所示:

thecolor2.style.backgroundColor = color

关于javascript - 在追加 child 之前添加CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070502/

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