<script type="text/javascript">
var watermarkText = "输入名称";
function WaterMarkOnBlur(textbox) {
if (textbox.value.length == 0) {
textbox.style.color = "gray";
textbox.value = watermarkText;
}
}
function WaterMartOnfocus(textbox)
{
if (textbox.value == watermarkText) {
textbox.style.color = "black";
textbox.value = "";
}
}
</script>
在TextBox应用这两个Javascript事件:
<asp:TextBox ID="TextBox1" runat="server" Text="输入名称"
ForeColor="Gray" onblur="WaterMarkOnBlur(this);"
onfocus="WaterMartOnfocus(this);">
</asp:TextBox>
演示: