English 中文(简体)
JQuery:鼠标悬停时更改图像按钮URL
原标题:JQuery: Change image button URL on mouse over

什么是jquery脚本来更改鼠标悬停按钮上的图像URL?

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/go.png" 
            PostBackUrl="~/Rep.aspx" />
最佳回答

你可以在CSS中完成:

<style type="text/css">
    #ImageButton1:hover { background-image: url( ~/your_url/here ) }
</style>
问题回答

将代码更改为

<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
            }
    });
});
$(document).ready(function(){
    $("input[type= image ]").hover(function(){
        $(this).attr("src","your/image.png");
    },function(){
        $(this).attr("src","your/old/image.png");
    });
});

我同意George Cummins上面的回答,但是,我必须切换到LinkButton并在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>

首先将其与(id或class)hover函数绑定

http://api.jquery.com/hover/

用attr更改图像的src

http://api.jquery.com/attr/

你可以试试这个。。。

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);
  }
);

您应该在脚本块中的按钮声明之后立即使用它。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签