Javascript DIV 背景问题
我目前有一个网站,我在其中使用一些 Javascript 来更改 DIV 的背景。
DIV 定义如下:
<td style="width: 750px; height: 300px; background-color: Black; border: 0px;">
<div id="outerscreen">
<div id="mainscreen">
</div>
</div>
</td>
我使用的 Javascript 如下:
var docEl = document.getElementById('outerscreen');
docEl.style.backgroundImage = "url('pics/intro.gif')";
我的问题是这个函数在 IE 上完美运行,但在 Chrome、FF 和 Safari 上失败。
任何人都可以阐明问题是什么以及我如何解决这个问题。
提前致谢 克里斯
I currently have a web site in which I am using some Javascript to change the Background of a DIV.
The DIV is defined as follows :
<td style="width: 750px; height: 300px; background-color: Black; border: 0px;">
<div id="outerscreen">
<div id="mainscreen">
</div>
</div>
</td>
and the Javascript I am using is as follows :
var docEl = document.getElementById('outerscreen');
docEl.style.backgroundImage = "url('pics/intro.gif')";
My problem arises where this function works perfectly on IE but fails for Chrome, FF and Safari.
Can anyone shed any light on what the problem is and how I might be able to resovle this.
Thanks in advance
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您在#mainscreen中有一些内容,最可能的解释是#mainscreen是浮动的,因此在计算其父级的高度时不使用它,但是您有一个触发的Doctype(或根本没有Doctype) Internet Explorer 中的 Quirks 模式,因此它不允许浮标从容器底部落下。
overflow:hidden
Assuming that you have some content inside #mainscreen, the most likely explanation is that #mainscreen is floated, so it isn't used when calculating the height of it's parent, but that you have a Doctype (or no Doctype at all) that triggers Quirks mode in Internet Explorer, so it doesn't allow floats to fall through the bottom of their containers.
overflow: hidden
on #outerscreen这在 FF4 和 Opera10 中有效。您需要指定
的高度和宽度,或者在其中包含一些占用屏幕空间的内容,才能使此效果可见。
This does work in FF4 and Opera10. You need to specify the height and width of
<div id="outerscreen">
, or have some content inside which takes up screen real estate for this effect to be visible.虽然我不建议在布局中使用表格,但这里有一个解决方案,它效果更好,并且
现在可以为您提供更多控制权:
当您想要进行更改时,在 css 中定义样式会更灵活,
代码分离总是好的
Although i don't recommend using a table for your layout here is a solution that works better and gives you more control
now the javascript:
Having your style defined in a css is more flexible when you want to make changes,
code seperation is always good
我认为问题在于 '
尝试:
I think the problem is with the '
try: