2011-05-24 38 views

回答

6

你可以做到這一點在CSS:

<style type="text/css"> 
    #ImageButton1:hover { background-image: url('~/your_url/here') } 
</style> 
+0

這是否適用於ASP.NET風格的ClientIDs? – 2011-05-24 13:15:14

+0

只要ASP.NET在不改變的情況下將ID傳遞給HTML文檔,它就可以工作。 – 2011-05-24 13:20:48

+0

@George不適合我。我嘗試添加.rollover:將{code}懸停在我的.css文件中並給出Imagebutton滾動類,但仍然無效。 – 2011-05-25 02:19:53

1
$(document).ready(function(){ 
    $("input[type='image']").hover(function(){ 
     $(this).attr("src","your/image.png"); 
    },function(){ 
     $(this).attr("src","your/old/image.png"); 
    }); 
}); 
0

你可以試試這個...

var old_image = ''; 

$('<%= this.ImageButton1.ClientID %>').hover 
(
    function() 
    { 
    old_image = $(this).attr('src'); 
    $(this).attr('src','/path/to/new_image'); 
    }, 
    function() 
    { 
    $(this).attr('src',old_image); 
    } 
); 

你應該使用它,腳本塊內的按鈕聲明之後。

3

改變你的代碼到這個

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/go.png" 
     PostBackUrl="~/Rep.aspx" data-imageover="~/Images/go-hover.png" /> 

,你可以有一個像多個圖像按鈕:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/go1.png" 
     PostBackUrl="~/Rep.aspx" data-imageover="~/Images/go1-hover.png" /> 
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/go2.png" 
     PostBackUrl="~/Rep.aspx" data-imageover="~/Images/go2-hover.png" /> 
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/Images/go3.png" 
     PostBackUrl="~/Rep.aspx" data-imageover="~/Images/go3-hover.png" /> 

然後添加腳本

$(function() { 
    // search all input type image, where data-imageover exists 
    $("input[type='image'][data-imageover]").each(function() { 
     $(this).hover(
      function() { // on mouseover 
       $(this).data("originalImg", $(this).prop("src")); // save original 
       $(this).prop("src", $(this).prop("data-imageover")); 
      }, 
      function() { // on mouseout 
       $(this).prop("src", $(this).data("originalImg")); // change to original 
      } 
    }); 
}); 
+0

使用HTML5屬性+1。 – 2011-05-24 13:15:39

+0

讚賞,他們確實存在的原因,甚至在IE6中工作正常,因爲它只是不關心代碼,再加上我的例子是使用'.data()'只是爲了踢它;) – balexandre 2011-05-24 13:17:39

1

我與喬治·康明斯回答以上同意,不過,我不得不切換到LinkBut​​ton並在CSS中定義按鈕尺寸:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="Sandbox_Asp.NET._4._0._Default" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
    <style type="text/css"> 
     .ImgBtnAdd 
     { 
      background-image: url('/images/add.png'); 
      background-repeat: no-repeat; 
      display: block; 
      height: 31px; 
      width: 60px; 
      border: 0; 
      outline: 0 !important; 
     } 
     .ImgBtnAdd:hover 
     { 
      background-image: url('/images/add_over.png'); 
     } 
    </style> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    Hover over the button: 
    <asp:LinkButton runat="server" ID="LinkButtonAdd" CssClass="ImgBtnAdd" 
     ToolTip="Add Button" Text="" /> 
</asp:Content>