• 通过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");
        }
    }

  • 相关阅读:
    jqgard改变单元格后重新定值(事件和弹窗)
    js多个input框赋相同值
    查看PHP已安装拓展的指令
    PHP重新安装zlib拓展,处理PHP Startup: Invalid library (maybe not a PHP library) 'zlib.so' in Unknown
    php拓展安装报错:PHP Startup: Invalid library (maybe not a PHP library) 'zlib.so' in Unknown
    Composer提示:Installation Failed, Reverting ./Composer.Json To Its Original Content.错误的解决办法
    SQL Server序列号的获取
    一步步开发Windows服务(Windows Service)[转]
    HTML+CSS+JS实现的贪吃球小游戏【转】
    自制一个滚动条
  • 原文地址:https://www.cnblogs.com/freeliver54/p/798374.html
Copyright © 2020-2023  润新知