2010-07-03 58 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

相關問題