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

求教这段图片切换函数为什么无效。
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>百度贴吧首页再现</title>

<style>

#img
{
width:300px;
height:200px;
overflow:hidden;
}

</style>



<script language="javascript">

function changeImg(num)
{
var _num=num;
var _img=document.getElementById("img");
var _img.src=new Array();
var _img.src[0]="0306fangyuan-01.jpg";
var _img.src[1]="0306fangyuan-02.jpg";
var _img.src[2]="20130305Android300200.png";
if(_num===1)
{
_img.getElementsByTagName("img").getElementById("00").src=_img.src[1];
};
alert("hello");

}

</script>

</head>

<body>

<div id="img">

<img id="00" src="0306fangyuan-01.jpg" />
<img id="01" src="0306fangyuan-02.jpg" />
<img id="02" src="20130305Android300200.png" />
</div>

<input type="button" onmousemove="" value="1" />
<input type="button" onclick="changeImg(1)" value="2" />

</body>
</html>

------解决方案--------------------


 _img.getElementsByTagName("img").getElementById("00").src=_img.src[1];
// _img.getElementsByTagName("img") 返回的是节点数组.你应该用一个节点来使用getElementById


------解决方案--------------------
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>百度贴吧首页再现</title>
 
<style>
 
#img
{
    width:300px;
    height:200px;
    overflow:hidden;
}
 
</style>
 
 
 
<script language="javascript">
var a = [
'http://www.baidu.com/img/baidu_jgylogo3.gif',
'0306fangyuan-02.jpg',
'20130305Android300200.png'
];

function changeImg(num)
{
    var _num=num;
var showimg = document.getElementById("showimg");
showimg.src = a[_num];
    alert("hello");
}
 
</script>
 
</head>
 
<body>
 
<div id="img">
<img id="showimg" src="http://www.baidu.com/img/baidu_jgylogo3.gif" />
</div>
 
<input type="button" onclick="changeImg(0)" value="1" />
<input type="button" onclick="changeImg(1)" value="2" />
<input type="button" onclick="changeImg(2)" value="3" />
 
</body>
</html>

------解决方案--------------------