TYPE 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>無標題文檔</title>
<style type="text/css">
*{margin:0; padding:0;}
.abc{width:720px; height:450px; margin:0 auto;
position:relative; left:0; top:0;}
.www{width:120px; height:20px; padding:2px 0 2px 9px;
position:absolute; right:20px; bottom:20px;}
.www ul li{list-style:none; float:left; margin-right:10px; background:#F60;
width:20px; height:20px; text-align:center; color:white;
cursor:pointer; border-radius:50%;}
</style>
<script type="text/javascript">
function dianji(a){
document.getElementById('pic').src=a+'.jpg';
document.getElementById('bg'+a).style.backgroundColor='black';
}
</script>
</head>
<body>
<div class="abc">
<img src="1.jpg" id="pic"/>
<div class="www">
<ul id="bbb">
<li id="bg1" onclick="dianji(1)">1</li>
<li id="bg2" onclick="dianji(2)">2</li>
<li id="bg3" onclick="dianji(3)">3</li>
<li id="bg4" onclick="dianji(4)">4</li>
</ul>
</div>
</div>
</body>
</html>
新手剛開始自學JS,學到帶參函數了,寫了一個點擊切圖,假設點擊②後大圖片變了,小②的背景變成了黑色,怎麼寫才能點擊③後②的黑色背景消失呢?就是點擊另一個切圖圖標的時候其他的圖標背景色恢復原狀。。。利用簡單地方法怎麼寫?新手求教了,謝謝了
用個全局變量記住上一個點擊的項,移除樣式即可
var lastId = false;
function dianji(a) {
if (lastId) document.getElementById('bg' + lastId).style.backgroundColor = '#F60';//
lastId = a;
document.getElementById('pic').src = a + '.jpg';
document.getElementById('bg' + a).style.backgroundColor = 'black';
}