我有以下简单的代码:
<div id="albumTitle">
<ul>
<li id="albumTitle1" class="selected"><a onClick="showAlbum('#albumTitle1','#album1')">Album One</a></li>
<li id="albumTitle2"><a onClick="showAlbum('#albumTitle1','#album1')">Album Two</a></li>
<li id="albumTitle3"><a onClick="showAlbum('#albumTitle1','#album1')">Album Three</a></li>
</ul>
</div>
现在专辑本身
<div id="albums">
<div id="album1">
<ul>
<li><img src="img/image1.png" /></li>
<li><img src="img/image2.png" /></li>
<li><img src="img/image3.png" /></li>
</ul>
</div>
<div id="album2" class="inactive">
<ul>
<li><img src="img/image1.png" /></li>
<li><img src="img/image2.png" /></li>
<li><img src="img/image3.png" /></li>
</ul>
</div>
<div id="album3" class="inactive">
<ul>
<li><img src="img/image1.png" /></li>
<li><img src="img/image2.png" /></li>
<li><img src="img/image3.png" /></li>
</ul>
</div>
</div>
所以这就是我想要的...
简单地说,当我单击一个专辑时,“已选择”类会转到该专辑标题,并且之前“已选择”类会被删除。
Likewise, when the ALBUM is selected, the "inactive" class is 'added' to the previously selected album and 'removed' from the newly selected album.
这是现在的代码:
<script language="javascript" type="text/javascript">
function showAlbum(albTitle, albName) {
var obj = new Array();
for (var x = 1; x <= 3; x++) {
obj.push("album" + x);
}
$.each(obj, function (key, value) {
//alert( key + ": " + value );
if ($("#" + value).hasClass("inactive")) {
$("#" + value).removeClass("inactive");
$("#" + albTitle).removeClass("selected");
} else {
$("#" + value).addClass("inactive");
$("#" + albTitle).addClass("selected");
}
});
}
</script>