【发布时间】:2014-10-02 04:57:20
【问题描述】:
最终产品:由 Word 文档中的数据填充的网页,该文档被馈送到循环的 HTML 结构中。
问题:运行时,会创建 HTML 元素,但 在调整窗口大小之前不会应用 CSS 类。
Java 脚本:
for (i=0; i<=timeline_data.length; i++){
var newParent = document.getElementById('wgt-timeline-bdy-wrap-id');
var newItem = document.createElement('div');
newParent.appendChild(newItem);
newItem.setAttribute('class','wgt-timeline-bdy-item');
var newText = document.createElement('p');
newItem.appendChild(newText);
newText.setAttribute('class','timeline-new-text');
newText.id="timeline-" + timeline_data[i].id + "-text";
}
CSS:
.wgt-timeline-bdy-item {
display: inline-block;
font-size: 20px;
position: relative;
text-align: center;
vertical-align: middle;
width: 100%;
}
.timeline-new-text{
font-size: 30px;
position: relative;
top:40px;
}
在 HTML 中没有什么意外,wgt-timeline-bdy-wrap-id 是一个 div。
提前感谢您的帮助
【问题讨论】:
-
for循环在您的代码中的确切位置?你能发布整个脚本吗? -
你是把 CSS 放在正文而不是头部吗?可能不是这样,但我以前见过这种情况。
-
你能做一个 jsfiddle 来演示这个问题吗? CSS 应该与静态内容一样适用于动态内容。
-
其实这也是 CSS 的重点之一。许多应用程序通过动态更改元素的类来更改元素的样式。
-
@Harry 但是循环也是创建新元素的地方。如果它在特定的事件处理程序中,则元素在该事件之前根本不会显示。
标签: javascript html css appendchild createelement