javascript - 为什么我不能对这个元素应用任何东西?
问题描述
所以我正在为魔方做秒表。我有一个div
元素(记录)应该显示单击按钮时我想输入的时间,但是我不能将p
元素附加到 this div
,也不能对其应用任何 CSS。
在 rubik.js 中:
document.getElementById("records").appendChild(document.createElement("p").appendChild(document.createTextNode(`${formula.first}.${formula.second}.${formula.third}`)));
当我在其中附加一个子段落时,div
它就变成了一个text
元素。CSS中的div
元素:
#records {
position: absolute;
bottom: 0;
right: 0;
width: calc(100% - 620px);
height: 130px;
border-top: 2px solid black;
background-color: white !important;
overflow-y: scroll;
}
这两个按钮(添加当前时间和删除上次时间按钮)有position
一个fixed
谢谢你的时间。
解决方案
该Node.appendChild()
方法返回附加的节点或文本节点。所以这个document.createElement("p").appendChild(document.createTextNode(
${formula.first}.${formula.second}.${formula.third}))
返回附加文本节点,而不是p
元素。
创建p
元素,将其分配给变量,然后附加文本节点,并将p
元素附加到#records
:
const formula = { first: '1', second: '2', third: '3' };
const p = document.createElement("p")
p.appendChild(document.createTextNode(`${formula.first}.${formula.second}.${formula.third}`))
document.getElementById("records").appendChild(p);
<div id="records"></div>
推荐阅读
- machine-learning - 为 1D CNN 创建和塑造数据
- python - 如何将 numpy 数组 [(x1,y1),(x2,y2),(x3,y3),.........(xn,yn)] 转换为 [(x1,y1,x1^2 ,y1^2),(x2,y2,x2^2,y2^2),.........(xn,yn,xn^2,yn^2)]
- amazon-web-services - 从我的网站中的 s3 存储桶流式传输视频是否会收费?
- c - 在 DECLARE_KFIFO 中使用联合背后的基本原理是什么
- amazon-web-services - 如何在 AWS appSync 的 Apache Velocity 请求映射模板中操作字符串和数组
- python - Python:将数据存储到文本文件
- hyperledger - 如何在超级账本卡尺中修复“缺少其“caliper.blockchain”字符串属性”
- google-apps-script - GAS 错误:TypeError:无法读取 null 的属性“getLastRow”(第 4 行,文件“Code2”)
- jquery - 如果列表中没有项目(通知),则切换 div
- javascript - 如何使用来自 for 循环的信息来编写电子邮件