Jquery:图像获胜'点击后不会改变,而是停留在悬停图像上.

Jquery: Image won't change after being clicked, instead it stays at hover image..

本文关键字:图像 改变 停留在 悬停 获胜 Jquery      更新时间:2023-09-26

我想在悬停时更改我的图像,然后当我单击悬停的图像时,我会将其更改为另一个图像。。但是,问题是,当我单击onhover图像时,它将保留在onhoverimage中,而不是将其更改为单击的图像。。

我的项目中有两个文件comp-profile.html和about.html,下面是源代码:

comp-profile.html:

$("#Content-header-wrapper #link1").unbind("hover").hover(function(){
            alert("asd");
            $("#Content-header-wrapper #link1").attr("src","Images/Content/link-about-hover.png");
        },function(){
            $("#Content-header-wrapper #link1").attr("src","Images/Content/link-about.png");
    });
$("#Content-header-wrapper #link1").unbind("click").click(function(){
        $("#Content-header-wrapper #link1").attr("src","Images/Content/link-comp-profile.png");
        $("#Content-header-wrapper #link2").attr("src","Images/Content/link-contact.png");
        $("#Content-header-wrapper #link1").attr("alt","link-comp-profile");
        $("#Content-header-wrapper #link2").attr("alt","link-contact");
        $("#Content-header").html("<img src='Images/Content/header-about.png' alt='header-about' width='273px' height='41'/>");
        $("#Content-fill").empty();
        $("#Content-fill").load("about-content.html");
        $("#Content").fadeOut(0);
        $("#Content").animate({opacity: 'toggle', height: 'toggle'},"slow");
    });

about.html:

$("#Content-header-wrapper #link1").unbind("hover").hover(function(){
            $("#Content-header-wrapper #link1").attr("src","Images/Content/link-comp-profile-hover.png");
        },function(){
            $("#Content-header-wrapper #link1").attr("src","Images/Content/link-comp-profile.png");
    });

另一个奇怪的想法是,每当我将图像悬停在about.html中时,它仍然会提醒"asd"。。。

请帮我做这件事。^^

hover不是事件,因此无法解除绑定,需要删除mouseentermouseleave事件。

$("#Content-header-wrapper #link1").off("mouseenter mouseleave").hover(function () {
    $("#Content-header-wrapper #link1").attr("src", "Images/Content/link-comp-profile-hover.png");
}, function () {
    $("#Content-header-wrapper #link1").attr("src", "Images/Content/link-comp-profile.png");
});