如何在jquery上更改鼠标上的链接图像

如何在jquery上更改鼠标上的链接图像,jquery,Jquery,我需要更改鼠标悬停时的链接图像。我使用了以下代码,但不起作用。也许问题在于图像源代码,但我仍然找不到代码的错误 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g

我需要更改鼠标悬停时的链接图像。我使用了以下代码,但不起作用。也许问题在于图像源代码,但我仍然找不到代码的错误

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head><title>Edit Document</title> 

<script src="../Scripts/jquery-1.3.2.js" type="text/javascript"></script> 
<script type="text/javascript"> 

        $(document).ready(function () {
        $("#lnkEdit").hover(
        function () {
            this.src = this.src.replace("../images/edit_off.gif", "../images/edit_on.png");
        },
        function () {
            this.src = this.src.replace("../images/edit_on.png", "../images/edit_off.gif");
        }
        );
    });

</script> 
</head> 
<body> 
<form method="post" action="Hover4.aspx" id="form1"> 
<div class="aspNetHidden"> 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTkwNjIyODQxZGR6kotcsbIsWRfokZrzasCtfPi0dxz4MBWWh9VxSJ6R0Q==" /> 
</div> 

<div> 
    <a id="HyperLink1" href="Hover4.aspx"><img src="../images/edit_off.gif" alt="HyperLink" /></a> 
</div> 
</form> 

编辑文档
$(文档).ready(函数(){
$(“#lnkEdit”)。悬停(
函数(){
this.src=this.src.replace(“../images/edit_off.gif”,”。/images/edit_on.png”);
},
函数(){
this.src=this.src.replace(“../images/edit_on.png”,”。/images/edit_off.gif”);
}
);
});

提前谢谢

  • 您正在代码中引用
    #lnkEdit
    ,但页面上没有具有该ID的元素
  • 您不需要
    replace
    或任何特殊功能,只需分配新的图像源即可
  • 试着这样做:

    JavaScript(根据评论更新)


    学分必须转到@casablanca,这应该可以:

    $(document).ready(function () {
      $.each($('#your_table_id > a'), functio(i, item) {
        $(item).hover(
        function () {
            $('img', this).attr('src', '../images/edit_on.png');
        },
        function () {
            $('img', this).attr('src', '../images/edit_off.png');
        });
      });
    
    });
    

    是的,你说得对。我忽略了第一项。我使用了你的代码,但仍然无法在悬停时进行更改。很抱歉,我错过了
    MyImage
    。现在已经修好了,谢谢你的密码。如果我使用了图像按钮,但仍然不能使用超链接,它就可以工作。超链接在服务器端超链接上运行,不允许我使用id=“MyImage”,因为它已经使用id=“Hyperlink1”任何其他建议?谢谢你的帮助。我已经更新了代码,因此它不需要图像ID-现在它直接引用链接中的
    标记。你的代码可以工作了。然而,我没有提到我想要在数据列表上有链接。当我使用您的代码时,它会替换表上的第一个链接,但当我将鼠标悬停在其他链接上时,其余链接不会更改。很抱歉没有提到我想要一个包含链接的列表,而不仅仅是一个链接。您甚至不需要将其包装在
    each()
    :大多数jQuery函数都适用于所有匹配的元素。我使用了代码,它不会更改单个图像。下面是更改:$(document).ready(function(){$.each($('ctl00'u ContentPlaceHolder1'u dlOpenRequests>a'),function(i,item){$(item).hover(function(){$('img',this).attr('src','../images/edit_on.gif');},function(){$('img',this.attr('src','../images/edit_off.gif'))谢谢你的帮助。
    $(document).ready(function () {
      $.each($('#your_table_id > a'), functio(i, item) {
        $(item).hover(
        function () {
            $('img', this).attr('src', '../images/edit_on.png');
        },
        function () {
            $('img', this).attr('src', '../images/edit_off.png');
        });
      });
    
    });