通过onmousedown和onclick 使 按钮 只响应 鼠标点击 事件

其目的大概与 之前一篇文章
屏蔽Button按钮对Enter回车键的响应 及实现Enter->Tab效果 
相似
都是为了防止
按钮对回车键等非点击动作
进行响应 而引起不必要的误操作

========== 前台页面 ============
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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 runat="server">
    <title>无标题页</title>
    <script language="javascript" type="text/javascript">
        var nowMouseClick = "";
       
        function fn_onmousedown()
        {
            nowMouseClick = event.srcElement.id;
        }
       
        function fn_onclick()
        {           
            if(nowMouseClick == event.srcElement.id)
            {
                nowMouseClick = "";
                return true;               
            }
            else
            {
                if(event.srcElement.id == "ImageButton1")
                {
                    document.all.Button1.focus();
                }
                else if(event.srcElement.id == "Button1")
                {
                    document.all.Text1.focus();
                }
                return false;
            }
        }
       
        function fn_Text1_onkeydown()
        {
            if(event.keyCode==13)
            {
                document.all.Text2.focus();
                event.keyCode = 0;
                event.returnValue = false;
            }
        }
        function fn_Text2_onkeydown()
        {
            if(event.keyCode==13)
            {
                document.all.ImageButton1.focus();
                event.keyCode = 0;
                event.returnValue = false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <input id="Text1" type="text" onkeydown="fn_Text1_onkeydown();"/>
        <input id="Text2" type="text"  onkeydown="fn_Text2_onkeydown();"/>
        <br />
        <asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </form>
</body>
</html>

========= 后台代码 =============

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        this.ImageButton1.Attributes.Add("onmousedown", "fn_onmousedown();");
        this.ImageButton1.Attributes.Add("onclick", "return fn_onclick();");
        this.Button1.Attributes.Add("onmousedown", "fn_onmousedown();");
        this.Button1.Attributes.Add("onclick", "return fn_onclick();");
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("ImageButton1 serve event");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("Button1 serve event");
    }
}

posted on 2007-06-28 08:26  freeliver54  阅读(2923)  评论(0编辑  收藏  举报

导航