我正在尝试使用 javascript/jquery 编写基本的图像交换。
出于某种原因,我的 if/else 语句运行不正确。我用错了吗?
谢谢
Javascript:
<script type="text/javascript">
var images = new Array();
var comic = document.getElementById("comicssubsite").src;
var artwork = document.getElementById("artworksubsite").src;
var about = document.getElementById("aboutsubsite").src;
images[0] = "./images/SiteDesign/Comics_subsites_hover.png";
images[1] = "./images/SiteDesign/Comics_subsites.png";
images[2] = "./images/SiteDesign/Artwork_subsites_hover.png";
images[3] = "./images/SiteDesign/Artwork_subsites.png";
images[4] = "./images/SiteDesign/About_subsites_hover.png";
images[5] = "./images/SiteDesign/About_subsites.png";
function onHover() {
if (comic) {
$("#comicssubsite").attr('src', images[0]);
}
else if (artwork) {
$("#artworksubsite").attr('src', images[2]);
}
else if (about) {
$("#aboutsubsite").attr('src', images[4]);
}
}
function offHover() {
if (comic) {
$("#comicssubsite").attr('src', images[1]);
}
else if (artwork) {
$("#artworksubsite").attr('src', images[3]);
}
else if (about)
$("#aboutsubsite").attr('src', images[5]);
}
}
</script>
HTML(片段)
<div class="span2">
<div id="pages">
<span class="pageitems" ><a href="./?action=homepage&page=1&site=comics"><img id="comicssubsite" src="./images/SiteDesign/Comics_subsites.png" alt="comics bg" onmouseover="onHover();" onmouseout="offHover();"/></a></span>
</div>
</div>
<div class="span2">
<div id="pages">
<span class="pageitems"><a href="./?action=homepage&page=1&site=artwork"><img id="artworksubsite" src="./images/SiteDesign/Artwork_subsites.png" alt="artwork bg" onmouseover="onHover();" onmouseout="offHover();"/></a></span>
</div>
</div>
<div class="span2">
<div id="pages">
<span class="pageitems"><a href="./?action=homepage&page=1"><img id="aboutsubsite" src="./images/SiteDesign/About_subsites.png" alt="about bg" onmouseover="onHover();" onmouseout="offHover();"/></a></span>
</div>
</div>