首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

缺少对象解决思路

2012-02-07 
缺少对象trTDIMGsrc images/2.jpg onclickshowDiv( div_used ) IMGsrc images/3.jpg oncl

缺少对象
<tr>
<TD>
<IMG   src= "images/2.jpg "   onclick   =   "showDiv( 'div_used ') "   > <IMG   src= "images/3.jpg "   onclick   =   "showDiv( 'div_current ') "> 2。当点击这两个图片时,再显示div里的图片。现在这行老是报错“缺少对象”
</TD>
</tr>
<tr>
<td>
<div   id   =   "div_current "   style   =   "display:block; "> <img   src= "images/banner.jpg "> 1。进入该html页时,先显示该图片。
</div>
<div   id   =   "div_used "   style   =   "display:none; "> <img   src   =   "images/4.jpg ">
</div>
</td>
</tr>
*********************************
3。JS
function   showDiv(divID)
{
var   id   =   document.getElementById(divID);
if(id   ==   'div_current ')
{
div_current.style.display   ==   "block ";
div_used.style.display   ==   "none ";
}
else
{
div_used.style.display   ==   "block ";
div_current.style.display   ==   "none ";
}
}

请大家帮帮忙,谢谢。

[解决办法]
function showDiv(divID)
{
if(divID == 'div_current ')
{
div_current.style.display == "block ";
div_used.style.display == "none ";
}
else
{
div_used.style.display == "block ";
div_current.style.display == "none ";
}
}
[解决办法]
var id = document.getElementById(divID);
if(id == 'div_current ')
这句话有问题啊!
你定义的id是一个object,而div_current看样子是个变量
[解决办法]
id = document.getElementById(divID)得到的是一个对象,不能与字符串比较
[解决办法]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<title> Untitled Page </title>
</head>
<body>
<table>
<tr>
<td style= "width: 398px; height: 136px "> div_used
<img src= "images/2.jpg " onclick= "showDiv( 'div_used ') " height= "50 " width= "50 "> </td>
<td style= "width: 398px; height: 136px "> div_current
<img src= "images/3.jpg " onclick= "showDiv( 'div_current ') " height= "50 " width= "50 "> </td>
</tr>
<tr>
<td style= "width: 398px; height: 138px ">
<div id= "div_current " style= "display: block; "> div_current
<img src= "images/banner.jpg " height= "50 " width= "50 ">
</div>
</td>
<td style= "width: 398px; height: 138px ">
<div id= "div_used " style= "display: none; "> div_used
<img src= "images/4.jpg " height= "50 " width= "50 ">


</div>
</td>
</tr>
<tr>
<td style= "width: 398px; height: 116px ">
</td>
<td style= "width: 398px; height: 116px ">
</td>
</tr>
<script language= "javascript " type= "text/javascript ">
function showDiv(divID)
{
var div_used = this.div_used;
var div_current = this.div_current;
if(divID== 'div_current ')
{
div_current.style.display = "block ";
div_used.style.display = "none ";
}
else
{
div_used.style.display = "block ";
div_current.style.display = "none ";
}
}
</script>
</table>
</body>
</html>

热点排行