簡體   English   中英

突出顯示焦點上的文本框服務器控件

[英]Highlighting an textbox server control on focus

我有一個文本框,點擊時我希望文本突出顯示:

<asp:TextBox ID="searchInput" runat="server" ></asp:TextBox>

我試圖通過C#添加功能:

protected void Page_Load(object sender, EventArgs e)
{
    searchInput.Attributes.Add("onfocus", "javascript:this.select();");
}

然而,我沒有得到理想的結果。 單擊該框時文本不突出顯示。 我做錯了什么,我該如何解決?

編輯:

澄清我想要選擇的文本。

當您單擊該框時,您可以更改插入符號位置,從而更改文本選擇范圍。

嘗試這個:

protected void Page_Load(object sender, EventArgs e)
{
    searchInput.Attributes.Add("onfocus", "var elem=this;setTimeout(function(){elem.select();},0);");
}

通過強制等待來自setTimeout()您可以在本機焦點事件觸發后select()文本,並將選擇范圍設置回文本value

示例小提琴: http//jsfiddle.net/mxn3F/

我總是通過將BackColor更改為黃色來突出顯示。 你可以嘗試它應該工作。

您應該嘗試以下代碼:

<asp:TextBox ID="searchInput" runat="server"  OnFocus="this.style.borderColor='red'" OnBlur="this.style.borderColor=''"></asp:TextBox>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM