Bergi(堆栈溢出用户)如此慷慨地帮助我想出了一个使用jQuery进行图像交换的解决方案。它现在悬停在悬停上,但是想要使它在当前子站点上保持突出显示(例如,我点击“漫画”页面,“漫画”保持突出显示)。那可能吗?谢谢!
<script type="text/javascript">
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"
],
"blog": [
"./images/SiteDesign/Blog_othersites_hover.png",
"./images/SiteDesign/Blog_othersites.png"
],
"facebook": [
"./images/SiteDesign/Facebook_othersites_hover.png",
"./images/SiteDesign/Facebook_othersites.png"
]
};
jQuery(document).ready(function($) {
$("#comicssubsite, #artworksubsite, #aboutsubsite, #blog, #facebook").hover(function(e) {
// mouseover handler
if (this.id in images) // check for key in map
this.src = images[this.id][0];
}, function(e) {
// mouseout handler
if (this.id in images)
this.src = images[this.id][1];
});
});
答案 0 :(得分:2)
Zotal Toth的答案应该有效,但你应该考虑更改你的mouseout-handler,除非你想让图像在用户盘旋时更改回来:
// mouseout handler
var section = document.location.href.split("/")[url.split("/").length-1].split(".")[0];
if (this.id in images) {
if this.id != section
this.src = images[this.id][1];
}
答案 1 :(得分:1)
以下假设您的网页名为comicssubsite.*
,artworksubsite.*
等:
var url = window.location.href;
for (key in images) {
if ( url.indexOf( key ) > -1 ) {
alert(key);
// highlight your menu item
$( "#" + key ).attr( "src", images[key][0] );
}
}