日期:2014-05-16  浏览次数:20465 次

菜鸟请教JS中层背景转换的问题
大家好,本人刚学习JS,看到了这段代码,但是调试的时候总是不能显示出背景,还请各位高手指教,代码哪里有问题呢,谢谢了。


<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function setBg(url)
{
document.bgexpro.background=url
}
</SCRIPT>
</HEAD>
<BODY>
<B>Layer Background</B>
<HR>
<LAYER NAME="buttons" top="50">
<FORM>
<INPUT TYPE="button" VALUE="First" onClick="setBg('first.gif')">
<BR>
<INPUT TYPE="button" VALUE="Two" onClick="setBg('two.gif')">
<BR>
<INPUT TYPE="button" VALUE="Three" onClick="setBg('three.gif')">
<BR>
<INPUT TYPE="button" VALUE="Four" onClick="setBg('four.gif')">
<BR>
<INPUT TYPE="button" VALUE="Five" onClick="setBg('five.gif')">
<BR>
</FORM>
</LAYER>
<LAYER NAME="bgexpro" BGCOLOR="blue" top="50" left="250" width="300" height="250">
<B><FONT COLOR="white">which may or may not read well with the various backgrounds.click the button</FONT></B>
</LAYER>
</BODY>
</HTML>

------解决方案--------------------
HTML code
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function setBg(url)
{ 
document.getElementById('test').style.backgroundImage="url(./"+url+")";
}
</SCRIPT>
</HEAD>
<BODY>
<B>Layer Background</B>
<HR>
<LAYER NAME="buttons" top="50">
<FORM>
<INPUT TYPE="button" VALUE="First" onClick="setBg('demo.jpg')">
<BR>
<INPUT TYPE="button" VALUE="Two" onClick="setBg('1.jpg')">
<BR>
<INPUT TYPE="button" VALUE="Three" onClick="setBg('three.gif')">
<BR>
<INPUT TYPE="button" VALUE="Four" onClick="setBg('four.gif')">
<BR>
<INPUT TYPE="button" VALUE="Five" onClick="setBg('five.gif')">
<BR>
</FORM>
</LAYER>
<LAYER id="test" NAME="bgexpro" BGCOLOR="blue" top="50" left="250" width="300" height="250">
<B><FONT COLOR="red">which may or may not read well with the various backgrounds.click the button</FONT></B>
</LAYER>
</BODY>
</HTML>

------解决方案--------------------
探讨
HTML code

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function setBg(url)
{
document.getElementById('test').style.backgroundImage="url(./"+url+")";
}
</SCRIPT>
</HEAD>
<BODY>
<B>Layer B……

------解决方案--------------------
HTML code

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function setBg(url)
{
    document.body.style.background="url("+url+")";
}
</SCRIPT>
</HEAD>
<BODY>
<B>Layer Background</B>
<HR>
<LAYER NAME="buttons" top="50">
<FORM>
<INPUT TYPE="button" VALUE="First" onClick="setBg('first.gif')">
<BR>
<INPUT TYPE="button" VALUE="Two" onClick="setBg('two.gif')">
<BR>
<INPUT TYPE="button" VALUE="Three&qu