失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 文本框获取和失去焦点默认值问题

文本框获取和失去焦点默认值问题

时间:2020-03-09 04:27:00

相关推荐

文本框获取和失去焦点默认值问题

1. HTML控件<input id="txtName" type="text" value="默认值" />

<script src="script/jquery-1.7.1.min.js" type="text/javascript"></script><script type="text/javascript">$(function () {$("#txtName").focus(function () {if ($(this).val() == "默认值") {$(this).val("");}}).blur(function () {if ($(this).val() == "") {$(this).val("默认值");}})});</script>

2.服务器控件.<asp:TextBox> 直接写在客户端上:

<asp:TextBox ID="txtName" runat="server" Text="默认值" OnFocus="javascript:if(this.value=='默认值') {this.value=''}"OnBlur="javascript:if(this.value==''){this.value='默认值'}"></asp:TextBox>

3.服务器控件写到C#服务器端上:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.Script.Serialization;namespace Web{public partial class Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){txtName.Attributes.Add("Value", "默认值");txtName.Attributes.Add("OnFocus", "if(this.value=='默认值') {this.value=''}");txtName.Attributes.Add("OnBlur", "if(this.value==''){this.value='默认值'}");}}}

总结. 单个文本框设置默认值时

如果觉得《文本框获取和失去焦点默认值问题》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。