我正在尝试使用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>
答案 0 :(得分:3)
为什么不使用 css
( oops,图片的背景图片)?
#comicssubsite {
background-image: ...
}
#comicssubsite:hover {
background-image: ...
}
请注意,您的标记无效,ID必须是唯一的,您应该使用类而不是ID,您还可以将ID更改为Comics
,Artwork
..并使用{{1} }和hover
方法。
attr
答案 1 :(得分:3)
您的if语句检查相应元素的src
属性是否为空。这不是你想要的 - 它们总是被执行。此外,如果您决定使用它,则应该在任何地方使用jQuery,尤其是在事件处理等DOM事件上。
您使用数组作为数据结构是一个良好的开端,但由于您仍然手动而不是以编程方式处理项目,因此无用。使用对象[literal]作为键值映射来获取图像ID的URL:
jQuery(document).ready(function($) {
var images = {
"comicssubsite": [
"./images/SiteDesign/Comics_subsites_hover.png",
"./images/SiteDesign/Comics_subsites.png"
],
"artworksubsite": [
"./images/SiteDesign/Artwork_subsites_hover.png",
"./images/SiteDesign/Artwork_subsites.png"
],
"aboutsubsite": [
"./images/SiteDesign/About_subsites_hover.png",
"./images/SiteDesign/About_subsites.png"
]
};
function mouseover(e) {
if (this.id in images) // check for key in map
this.src = images[this.id][0];
}
function mouseout(e) {
if (this.id in images)
this.src = images[this.id][1];
}
$("#comicssubsite, #artworksubsite, #aboutsubsite").hover(mouseover, mouseout);
// You might want to use a loop instead, then you don't need to write the ids twice:
// for (var id in images)
// $('#'+id).hover(mouseover, mouseout);
});
答案 2 :(得分:2)
您的第一个if
语句始终返回true
,因为每个图片代码都有一个来源。
如果你有这个:
var comic = document.getElementById("comicssubsite").src;
然后这个:
if (comic) {
//code
}
所有if语句都在检查漫画是否存在,它确实存在。