某一时候,为文本框(TextBox)装饰个水印。它有两种状态,一是blur和focus。因此,我们可以在Javascript写两个事件:

1 <script type="text/javascript"> 2 var watermarkText = "输入名称"; 3 4 function WaterMarkOnBlur(textbox) { 5 if (textbox.value.length == 0) { 6 textbox.style.color = "gray"; 7 textbox.value = watermarkText; 8 } 9 } 10 11 function WaterMartOnfocus(textbox) 12 { 13 if (textbox.value == watermarkText) { 14 textbox.style.color = "black"; 15 textbox.value = ""; 16 } 17 } 18 </script>
在TextBox应用这两个Javascript事件:

1 <asp:TextBox ID="TextBox1" runat="server" Text="输入名称" 2 ForeColor="Gray" onblur="WaterMarkOnBlur(this);" 3 onfocus="WaterMartOnfocus(this);"> 4 </asp:TextBox>
演示: