喜悦国际村 » XML/HTML/CSS/Js » 求教!如何用js更换td标签的背景图片

页: [1]
芒果2004-4-24 06:01 AM
求教!如何用js更换td标签的背景图片

我在做导航栏的时候想要让鼠标经过时变换TD标签的背景图片
但是不知道为什么,鼠标指上去的时候,原来图片就会不见了
而新的图片并没有显示出来,看错误提示是参数无效,折腾了
半天,也没有解决掉,所以到此地请教一下各位高手,拜托了!

我写的代码:

<script language="JavaScript1.2">
function change(){
source=event.srcElement
if (source.tagName=="TR"||source.tagName=="TABLE")
return
while(source.tagName!="TD")
source=source.parentElement
if (source.style.background!="a.gif"&&source.id!="ignore")
source.style.background="a.gif"
}

function changeback(){
if (event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore")
return
if (event.toElement!=source)
source.style.background="b.gif"
}
</script>

<table width="160" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="160" height="24" align="center" valign="middle" background="b.gif" onMouseover=change() onMouseout=changeback()>
      链接1
    </td>
  </tr>

  <tr>
    <td width="160" height="24" align="center" valign="middle" background="b.gif" onMouseover=change() onMouseout=changeback()>
      链接2
    </td>
  </tr>

</table>

lijian05512004-4-27 06:48 AM
看看这样是不是简便些

<td onmouseover="document.images['changeimage11'].src='Image/11-2.gif'" onmouseout="document.images['changeimage11'].src='Image/11-1.gif'"><a href="http://www.ysoo.com/" target=_blank><img name="changeimage11" src="Image/11-1.gif" width=75 height=25 border=0></a></td>

芒果2004-5-1 09:53 AM
谢谢了,不过可能是把我的意思弄错了,我的目的是做动态的导航栏,是要让TD标签的背景图片产生变动,如果是用IMG标签的话,我就不能动态的写字在上面了

longhorn2004-5-1 02:02 PM
<table width=100>
<tr height=50>
<td style='background-image:url("http://www.phpx.com/happy/images/avatars/icone25.gif");width:100%'
onMouseover="this.style.backgroundImage='url([url]http://www.phpx.com/happy/avatar.php?userid=17889&dateline=1074682894[/url])'" onMouseout="this.style.backgroundImage='url([url]http://www.phpx.com/happy/images/avatars/icone25.gif[/url])'">skjdfksjdfksjdkfjskfjskdjfksdfdjfksjd</td>
</tr>
</table>


查看完整版本: 求教!如何用js更换td标签的背景图片


Powered by Discuz! Archiver 6.1.0  © 2001-2006 Comsenz Inc.
Processed in 0.005903 second(s), 2 queries