请大家帮我看看,为什么下面代码浏览器第一次执行会有问题,比如ie、firefox、谷歌等都是这样,需要刷新一下才正常。什么原因造成的?应该怎么改。谢谢~~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style>
#div1 {
width:500px;
height:500px;
}
</style>
<script>
function displayImage(XelementId,XimageSrc)
{
var image=new Image();
image.src=XimageSrc;
var div=document.getElementById(XelementId);
div.style.background="url("+XimageSrc+") no-repeat";
div.style.backgroundPosition=(500-image.width)/2+"px "+(500-image.height)/2+"px";
}
</script>
</head>
<body onload='displayImage("div1","2.jpg")'>
<div id="div1"></div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style>
#div1 {
width:500px;
height:500px;
}
</style>
<script>
function displayImage(XelementId,XimageSrc)
{
var image=new Image();
image.src=XimageSrc;
var div=document.getElementById(XelementId);
div.style.background="url("+XimageSrc+") no-repeat";
div.style.backgroundPosition=(500-image.width)/2+"px "+(500-image.height)/2+"px";
}
</script>
</head>
<body onload='displayImage("div1","2.jpg")'>
<div id="div1"></div>
</body>
</html>
div.style.backgroundPosition=(500-image.width)/2+"px "+(500-image.height)/2+"px";
<script>
function displayImage(XelementId,XimageSrc)
{
var image=new Image();
image.src=XimageSrc;
var div=document.getElementById(XelementId);
div.style.background="url("+XimageSrc+") no-repeat";
image.onload=function(){
div.style.backgroundPosition=(200-image.width)/2+"px "+(200-image.height)/2+"px";
}
}
</script>
[code=HTML][function displayImage(XelementId,XimageSrc)
{
var image=new Image();
image.src=XimageSrc;
var div=document.getElementById(XelementId);
div.style.background="url("+XimageSrc+") no-repeat";
div.style.backgroundPosition=(500-image.width)/2+"px "+(500-image.height)/2+"px";
image.onload=function(){
div.style.backgroundPosition=(500-image.width)/2+"px "+(500-image.height)/2+"px";
}
}/code]
<script>
function displayImage(XelementId,XimageSrc)
{
var image=new Image();
var div=document.getElementById(XelementId);
div.style.background="url("+XimageSrc+") no-repeat";
image.onload=function(){
div.style.backgroundPosition=(200-image.width)/2+"px "+(200-image.height)/2+"px";
}
image.src=XimageSrc;
}
</script>