如何在頁面中添加焦點 JavaScript執行個體

來源:互聯網
上載者:User

        細心的朋友不難發現,人性化的網站都是進行過焦點處理的。即可以通過斷行符號鍵來完成確定或提交功能。
而通常情況下,預設按鈕是沒有焦點的,這就需要我們通過一些方法把焦點加上。下面就簡單介紹一下Js添加
焦點的方法。

/// <summary>
    /// 添加焦點
    /// </summary>
    private void Script()
    {
        //執行個體化一個StringBuilder執行個體
        StringBuilder sb = new StringBuilder();    
        //找到頁面中的各個控制項
        Button btnOK = this.FindControl("btn_AddSec") as Button;
        TextBox txtcode = this.FindControl("txt_SchoolCode") as TextBox;
        TextBox txtname = this.FindControl("txt_SchoolName") as TextBox;
        TextBox txtsec = this.FindControl("txt_TotalNum") as TextBox;
        //添加JS函數
        sb.Append("function f()\n{\n");
        sb.Append("var finishbtn;\n");
        sb.Append("finishbtn = document.getElementById('" + btnOK.ClientID + "');\n");
        sb.Append("if(event.keyCode=='13')\n{\n");
        sb.Append("if(finishbtn!=undefined){\n");
        sb.Append("finishbtn.click();\n");
        sb.Append("return false;\n");
        sb.Append("}\n");
        sb.Append("}\n");
        sb.Append("}\n");
        //將相應控制項的屬性加上此函數
        txtcode.Attributes["onkeypress"] = "return f();";
        txtname.Attributes["onkeypress"] = "return f();";
        txtsec.Attributes["onkeypress"] = "return f();";
        //註冊
        Page.ClientScript.RegisterClientScriptBlock(typeof(string), Guid.NewGuid().ToString(), sb.ToString(), true);
    }

     然後只需要在相應的地方調用就可以了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.