2012-12-01 69 views
0

我想要做的是在足够大的屏幕上显示全屏幕背景图像,并且它在大多数浏览器上都能正常工作,但是会导致其他问题。我通过W3 Validator运行它,它说XHTML不支持document.write中的Div和Img标签。如果没有document.write,我该怎么编码,以便它可以通过验证器,而不会在某些浏览器上导致错误?如何从XHTML中的Document.Write中删除Div和Img标签

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.7.1.min.js"></script> 
<script type="text/javascript" src="jquery.fullscreenBackground.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
     $("#background-image").fullscreenBackground(); 
    }); 
</script> 

      <script type="text/javascript"> 
var viewPortWidth = $(window).width(); 
var viewPortHeight = $(window).height(); 
if (viewPortHeight >= 300 && viewPortWidth >= 400) { 
document.write("<div id='background-image'><img src='BGLarge.gif' alt='background' width='1920' height='1080' ></div>"); 
} 
else { 
document.write(""); 
} 
</script> 
+0

是什么fullscreenBackground()呢? – jtheman

+0

考虑使用'<!DOCTYPE html>' –

+0

它从jquery.fullscreenBackground.js调用代码 – user1059021

回答

2
if (viewPortHeight >= 300 && viewPortWidth >= 400) { 
    $('body').prepend('<div id="background-image"><img src="http://thetylerpress.com/new/BGLarge.gif" alt="background" width="1920" height="1080" ></div>'); 
} 

你不会需要else语句

+0

非常感谢!我唯一需要改变的是'body'到'html',它的工作非常完美。 – user1059021

1

这将是有益知道哪些浏览器引发错误。无论如何,为什么不使用jQuery来实现你想要的?

$('body').prepend(
    $('<div></div>') 
     .attr('id', 'background-image') 
     .html("<img src='http://thetylerpress.com/new/BGLarge.gif' alt='background' width='1920' height='1080' >") 
);​ 

活生生的例子:http://jsfiddle.net/Wyd43/