0

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];
            });
        });
4

2 回答 2

2

Zotal Toth 的答案应该有效,但是您应该考虑像这样更改鼠标处理程序,除非您希望图像变回来,一旦用户将鼠标悬停在它上面:

// 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];
}
于 2012-11-12T02:02:33.257 回答
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] );
    }
}
于 2012-11-12T01:58:01.347 回答