突出显示焦点上的文本框服务器控件
本文关键字:文本 服务器控件 显示 焦点 | 更新日期: 2023-09-27 18:36:52
我有一个文本框,单击时我希望文本突出显示:
<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/
我总是通过将背景颜色更改为黄色来突出显示。你可以试试它应该工作。
您应该尝试以下代码:
<asp:TextBox ID="searchInput" runat="server" OnFocus="this.style.borderColor='red'" OnBlur="this.style.borderColor=''"></asp:TextBox>