JS切换图片问题

<script type="text/javascript">

function shubiao(i)
{
if (i)
{
document.getElementById("da"+i).className = "zuobo1";
document.getElementById("dian"+i).className = "youbo1";
}
else
{
document.getElementById("da"+i).className = "zuobo2";
document.getElementById("dian"+i).className = "youbo2";
}

}
</script>
<body>
<div class="bofang" >
<div class="zuobo1" id="da1"><img src="image/jyzt/1.jpg" /></div>
<div class="zuobo2" id="da2"><img src="image/jyzt/2.jpg" /></div>
<div class="zuobo2" id="da3"><img src="image/jyzt/3.jpg" /></div>

<div class="youbo">
<div class="youbo1" id="dian1" onmouseover="shubiao(1)"></div>
<div class="youbo2" id="dian2" onmouseover="shubiao(2)"></div>
<div class="youbo2" id="dian3" onmouseover="shubiao(3)"></div>
</div>
<div style="clear:both"></div>
</div>
</body>
我想要效果是图中鼠标移到左边色块就会切换不同图片,我写的JS不知道为什么不行 错在哪 谢谢

第1个回答  2011-09-23
下面用jquery写的。你这个和选项卡类似:
<!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=utf-8" />
<title>Jane shopping</title>
<script src="http://www.w3school.com.cn/jquery/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$(".rightbox").click(function(){//选取class为rightbox的元素
$(this).addClass("this").siblings().removeClass("this")//给选中的当前元素加上.this,同时同辈元素(.rightbox)中的.this
$this =$(".right .rightbox");
$sy = $this.index(this)//取得当前索引
$(".left .leftbox").eq($sy).show().siblings().hide()//显示同.rirhtbox索引相同的。leftbox的DIV,并隐藏同辈.leftbox的DIV
});
})
</script>
<style type="text/css">
*{ margin:0; padding:0;}
.testbox{ width:600px; height:250px; border:1px solid green;}
.left{ float:left; width:400px;}
.leftbox{ widht:390px; height:230px; border:1px solid #333; margin:10px 0 0 10px; display:none;}
.right{ float:left; width:120px; margin:10px 0 0 10px;}
.rightbox{ width:110px; background:#eee; height:50px; margin-top:10px;cursor:pointer;}
.this{ border:2px solid blue;}
</style>
</head>
<body>
<div class="testbox">
<div class="left">
<div class="leftbox" style="display:block;">图片1</div>
<div class="leftbox">图片2</div>
<div class="leftbox">图片3</div>
</div>
<div class="right">
<div class="rightbox">小图1</div>
<div class="rightbox">小图2</div>
<div class="rightbox">小图3</div>
</div>
</div>
</body>
</html>
第2个回答  推荐于2016-08-25
<script type="text/javascript">

function shubiao(i)
{
for(var j=1;j<4;j++){
document.getElementById("da"+j).className = "zuobo2";
document.getElementById("dian"+j).className = "youbo2";
}
document.getElementById("da"+i).className = "zuobo1";
document.getElementById("dian"+i).className = "youbo1";

}
</script>
<body>
<div class="bofang" >
<div class="zuobo1" id="da1"><img src="image/jyzt/1.jpg" /></div>
<div class="zuobo2" id="da2"><img src="image/jyzt/2.jpg" /></div>
<div class="zuobo2" id="da3"><img src="image/jyzt/3.jpg" /></div>

<div class="youbo">
<div class="youbo1" id="dian1" onmouseover="shubiao(1)"></div>
<div class="youbo2" id="dian2" onmouseover="shubiao(2)"></div>
<div class="youbo2" id="dian3" onmouseover="shubiao(3)"></div>
</div>
<div style="clear:both"></div>
</div>
</body>追问

纠结 为什么要加这个for(var j=1;j<4;j++){

追答

初始化

追问

能解释下这个为什么会这样换图片吗?就是代码的是上面意思 嘻嘻 谢谢了

追答

for循环初始化;后面是点击效果。

追问

呵呵 谢谢

本回答被提问者采纳
相似回答
大家正在搜