动态创建Div,在其他Div之上
Create Div on the fly, above the others
这个脚本每秒钟都会使用AJAX打开一个页面,并将内容返回到这个页面上的一个新div中,这个div是它动态创建的。但不幸的是,它们是相互重叠的。我想在顶部创建每个新的div。我真的不想使用jquery或类似的东西。
对此的任何帮助都非常感激,我对JS不是很有信心,所以如果不是很明显,你能给我一个小小的解释吗?:)谢谢
function timedCount()
{
min = Math.floor(s/60);
sec = s-(min*60);
if(sec < 10) { sec = '0'+sec; }
if(min >= 60) { min = min-15; }
if(quit == 0) { document.getElementById('mTime').innerHTML = min+':'+sec; }
s=s+1;
var ajaxRequest;
try { ajaxRequest = new XMLHttpRequest(); } catch (e){
try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4 && ajaxRequest.responseText != '')
{
if(ajaxRequest.responseText == 'HT') {
document.getElementById('mTime').innerHTML = 'Half Time';
t=setTimeout("timedCount()",(3600-s)*1000);
quit=1;
return;
}
if(ajaxRequest.responseText == 'FT') {
document.getElementById('mTime').innerHTML = 'Full Time';
quit=1;
return;
}
el = document.createElement('rep'+i);
el.innerHTML = ajaxRequest.responseText +'<br>';
document.getElementById('container').appendChild(el);
i=i+1;
}
}
ajaxRequest.open("GET", "getMatch.php", true);
ajaxRequest.send(null);
}
您试过使用insertBefore
吗?
你可以改变行:
document.getElementById('container').appendChild(el);
var container = document.getElementById('container');
container.insertBefore(el, container.firstChild);
container.firstChild
将为您提供对容器内第一个元素的引用,并将el
放在其前面。
您需要insertBefore函数
var parent = document.getElementById('container');
parent.insertBefore(el, parent.firstChild);
注意,如果parent为空,则parent。因此,firstChild为null,然后el将插入到parent的末尾,这是您想要的。
appendChild方法总是在集合的末尾添加新节点,这就是为什么它出现在底部-参见http://msdn.microsoft.com/en-us/library/ms535934(v=VS.85).aspx
使用这里记录的insertBefore方法:http://msdn.microsoft.com/en-us/library/ms535934(v=VS.85).aspx。你可以使用document.getElementById('container')。firstChild作为第二个参数
通常,如果我真的想在所有其他元素上放置一个div,我会使用css position和z-index,比如:
div{
position: absolute;
z-index: 9999;
}
:)
相关文章:
- 如何在不影响其他元素的情况下扩展DIV
- 将DIV包裹在锚标签内或其他方式
- 上次创建的DIV高于其他DIV
- Div向右对齐,并根据右侧的其他Div更改x位置
- 使用 Javascript 悬停将 Div 复制到页面上的其他位置
- 防止固定 Div 覆盖其他 Div
- 动态 Div 宽度影响其他 Div 宽度
- DIV 覆盖/重叠其他元素
- Div 在 Chrome 中的位置与其他浏览器不同
- JQuery 淡入/淡出会导致其他 DIV 闪烁
- 悬停在第一个 DIV 上会影响所有其他第一个 DIV
- 将 DIV(由类指定)附加到其他 DIV(由其他类指定)中
- 刷新单个Div,而不刷新其他内容
- 动态创建的DIV在其他创建按钮中的位置
- 我如何才能使我的早期DIV不被其他后期DIV隐藏
- 链接无法在Ipad上的Div上工作-任何其他设备/浏览器都可以100%工作
- 悬停在其他DIV上时显示DIV,该DIV不是CSS或JS的父级
- 点击关闭Div不适用于Chrome,但适用于所有其他浏览器
- 我的谷歌地图DIV位于其他所有内容的顶部-我不明白为什么
- 如果Div被选中- hasClass -触发一些事件,如果没有-触发其他事件.这可以作为独立函数来实现吗