简体   繁体   English

为什么我不能对这个元素应用任何东西?

[英]Why can't I apply anything to this element?

So I'm making my stopwatch for Rubik's cube.所以我正在制作魔方的秒表。 I have a div element (records) that should display the time which I want to enter when a button is clicked, but I can't append a p element to this div , and neither can I apply any CSS to it.我有一个div元素(记录),它应该显示单击按钮时我想输入的时间,但我无法将p元素附加到这个div ,也不能对其应用任何 CSS。

in rubik.js:在 rubik.js 中:

document.getElementById("records").appendChild(document.createElement("p").appendChild(document.createTextNode(`${formula.first}.${formula.second}.${formula.third}`)));

When I append a child paragraph to this div it becomes a text element.当我将一个子段落附加到这个div它变成了一个text元素。 the div element in CSS: 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;
}

The two buttons(the add current time and the remove last time buttons) have a position of fixed两个按钮(添加当前时间删除上次时间按钮)的positionfixed

Thanks for your time.谢谢你的时间。

The Node.appendChild() method returns the appended node or text node. Node.appendChild()方法返回附加节点或文本节点。 So this document.createElement("p").appendChild(document.createTextNode( ${formula.first}.${formula.second}.${formula.third} )) returns the append text node, and not the p element.所以这个document.createElement("p").appendChild(document.createTextNode( ${formula.first}.${formula.second}.${formula.third} ))返回追加文本节点,而不是p元素.

Create the p element, assign it to variable, then append the text node, and append the p element to #records :创建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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM