日期:2014-05-17  浏览次数:20640 次

如何在javascript中改变div背景图片?

div如下:
<div onmouseover="changeImg(this)">
</div>
javascript方法:
function changeImg(obj){
   obj.style.backgroundImage = "url(img/yygk1.png)";
   //obj.style.backgroundImg = "url(img/yygk1.png)";
   //obj.style.background = "url(img/yygk1.png)";
}

div原本是有背景图片的,当鼠标移动到div上的时,期望改变背景图片为yygk1.png,但实际运行结果是背景图片没了,这是怎么回事呢?
javascript div背景图片

------解决方案--------------------
不需要定义,LZ写的URL正确
又测试了一次,可以正确执行。
<div onmouseover="changeImg(this)" style="background-image:url(/xi.jpg);">
asdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklasasdjasldjlsdjlksjdklsjksldjklas
</div>
<script type="text/javascript">

function changeImg(obj){
   obj.style.backgroundImage = "url(img/1.png)";
   //obj.style.backgroundImg = "url(img/yygk1.png)";
   //obj.style.background = "url(img/yygk1.png)";
}

</script>