javascript 创建 html 对象时 css top 出现问题
我正在 JavaScript 中创建一个跨度以附加到 td。效果很好。然而,由于某种原因,当我调用
document.getElementById("myTd").appendChild(thisNewSpanObject);
新的跨度时,似乎认为它是窗口的子窗口。因此,当我设置 span 的属性时
top:-10px;
,它实际上是在页面之外,但与它应该在的位置水平对齐,而实际上,我只是希望它在没有分配 css 属性的情况下在加载位置上方显示 10 个像素到它。我应该在这里使用除 top 之外的其他东西吗?如果我不使用 top,则跨度会加载到位,低 10 像素(位置:绝对;已设置)。
I am creating a span in javascript to append to a td. Works great. However, for some reason when I call
document.getElementById("myTd").appendChild(thisNewSpanObject);
the new span seems to think it is a child of the window. So when I set the attribute of
top:-10px;
the span is actually off the page, yet aligned horizontally with where it should be, when in reality, I just want it to display 10 pixels above where it would load if it did not have the css property assigned to it. Should I be using something other than top here? If I don't use top then the span loads right in place, 10 pixels too low (position:absolute; is set).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
position:relative
添加到span
元素。您当前正在使用
position:absolute
,这意味着它将相对于其最近的祖先,除了position: static
(元素的默认值)或文档之外。Add
position: relative
to thespan
element.You are currently using
position: absolute
, meaning it will be relative to its nearest ancestor with something besidesposition: static
(the default for elements) or the document.将
position:relative
添加到 ID 为myTd
的元素,而不是span
。Add
position: relative
to the element with IDmyTd
, not thespan
.