appendChild在IE中无法正常工作 [英] appendChild doesn't work correctly in IE
问题描述
我正在尝试创建一个简单的模态窗口,但IE不合作。当我在IE中调用此功能时,内容将显示在所有内容下的页面底部,并且不会显示叠加图像。这是代码:
I am trying to create a simple Modal window, but IE isn't cooperating. When I call this function in IE, the content appears at the bottom of the page under all content and the overlay image does not appear. Here's the code:
function applyOverlay(src)
{
var my_overlay = document.createElement('div');
my_overlay.setAttribute('id','myoverlay');
var doc_height = document.body.scrollHeight;
my_overlay.setAttribute('style','text-align:center; position:fixed; top:0px; left:0px; background-image:url("images/trbg.png"); width:100%; height:'+doc_height+'; z-index:999;');
my_overlay.innerHTML="<iframe style='background:none;' frameborder=0 height='100%' width='80%' src='"+src+"'><iframe>";
document.body.appendChild(my_overlay);
}
推荐答案
这是常见的IE问题。它很刺激,但是可以管理。
This is common IE issue. It is irritating, but managable.
如果
document.body.appendChild 在正文关闭之前在body标签内执行,IE6将只是不加载页面。 7和8将等到页面加载If document.body.appendChild is executed within the body tag before the body is closed, IE6 will simply not load the page. 7 and 8 will wait until the page is loaded
那么,如何解决这个问题?
So, how to approach this issue?
- 等待主体加载,使用 body.onload 。
- 将元素追加到另一个元素而不是body标签。
我推荐第二个选项。将元素附加到另一个目标元素将保留预期的行为,而不会改变您将元素添加到网站的方式。
I recommend the second option. Appending elements to another target element will preserve the intended behavior and not change the way you add your elements to the site.
这篇关于appendChild在IE中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!