繁体   English   中英

如何从XHTML中的Document.Write删除Div和Img标签

[英]How To Remove Div and Img Tags From Document.Write in XHTML

我想做的是在足够大的屏幕上显示全屏背景图像,并且该图像可以在大多数浏览器上使用,但在其他浏览器上却引起问题。 我通过W3验证程序运行它,并说XHTML在document.write中不支持Div和Img标记。 没有document.write的情况下,我该如何编码,使其通过验证程序,而不会在某些浏览器上引起错误?

<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>

使用jquery前置元素: http : //api.jquery.com/prepend/

这应该工作。 或者,您可以更改您的文档类型。

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语句

知道哪个浏览器会引发错误将很有用。 无论如何,为什么不使用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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM