2010-07-03 57 views
1

我需要更改鼠标悬停上的链接图像。我使用了下面的代码,但不起作用。也许问题出在图像源上,但我仍然无法找到他的代码有什么问题。如何更改鼠标上的链接图像上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><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> 

在此先感谢。

回答

3
  1. 您正在引用您的代码#lnkEdit但与页面
  2. 对ID没有元素你不需要replace或任何特殊功能,你可以简单地分配新的图像源。

尝试是这样的:

的JavaScript(更新每评论)

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

是,你是对的。我忽略了第1项。 我使用了您的代码,但仍然无法在悬停时进行更改。 – blueDroid 2010-07-03 03:49:16

+0

对不起,我错过了'MyImage'的''''。现在已经修复了。 – casablanca 2010-07-03 04:01:27

+0

感谢您的代码。它适用于如果我使用图像按钮,但仍然不适用于超链接。超链接运行在服务器端 HyperLink 并且不让我使用id =“MyImage”,因为它已经使用id =“Hyperlink1” 任何其他建议? 感谢您的帮助。 – blueDroid 2010-07-06 13:44:40

0

学分必须去@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'); 
    }); 
    }); 

}); 
+0

你甚至不需要用'each()'来包装它:大多数jQuery函数总是适用于所有匹配的元素。 – casablanca 2010-07-07 02:12:14

+0

我使用了代码,它不会更改单个图像。下面是变化: $(文件)。就绪(函数(){$ 。每个($( '#ctl00_ContentPlaceHolder1_dlOpenRequests> A'),功能(I,项目){$ (项目).hover( 功能(){ $('img',this).attr('src','../images/edit_on。GIF'); }, function(){('img',this).attr('src','../images/edit_off.gif'); }); }); 感谢您的帮助。 – blueDroid 2010-07-07 12:40:23

相关问题