首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

急 ASP.NET中页面加载时文本框内有文字 取得焦点时文字消失

2012-10-10 
急 ASP.NET中页面加载时文本框内有文字 获得焦点时文字消失script typetext/javascript languagejav

急 ASP.NET中页面加载时文本框内有文字 获得焦点时文字消失
<script type="text/javascript" language="javascript">
function cls() {  
  with (event.srcElement)  
  if (value == defaultValue) {
  value = ""
  this.style.color = '#000000'
  }
  }
  function res() {  
  with (event.srcElement)  
  if (value == "") {
  value = defaultValue
  this.style.color = '#808080'
   
  }
  } 
  </script>

<asp:TextBox ID="tbname" runat="server" Width="128px" TabIndex="1" Text="用户名" onfocus="cls()" onblur="res()" ForeColor="Gray"></asp:TextBox>
<asp:TextBox ID="tbpassward" runat="server" Width="128px" TabIndex="1" Text="请输入密码" onfocus="cls()" onblur="res()" ForeColor="Gray"></asp:TextBox>

我上面这种方式 页面加载时文本框内有文字 获得焦点时文字消失的功能是可以实现的,但是 当获得焦点时 文字的颜色还是gray 应该是黑色的才对

2:<asp:TextBox ID="tbname" runat="server" Width="205px" Text="用户名" OnFocus="javascript:if(this.value=='用户名') {this.value='';this.style.color='#000000'}" OnBlur="javascript:if(this.value==''){this.value='用户名;
 
this.style.color='#808080'}" ForeColor="Gray"></asp:TextBox>
这种方式 是可以的 但是也没感觉和上面的有什么不同呀
还有一个问题就是 <asp:TextBox ID="tbpwd" runat="server" Width="128px" TextMode="Password" 
  TabIndex="2" Text="密码" OnFocus="javascript:if(this.value=='密码) {this.value='';this.style.color='#000000'}" OnBlur="javascript:if(this.value==''){this.value='密码;
 
this.style.color='#808080'}" ForeColor="Gray"></asp:TextBox> 如果TextMode="Password" 那么好像不起作用,请大家帮忙看看 谢谢

[解决办法]
'密码少了“'”
[解决办法]
楼主,我测试了一下,你的代码如果这样写

JScript code
<asp:TextBox ID="tbname" runat="server" Width="128px" TabIndex="1" Text="用户名" onfocus="cls()" onblur="res()" ForeColor="Gray"></asp:TextBox><asp:TextBox ID="tbpassward" runat="server" Width="128px" TabIndex="1" Text="请输入密码" onfocus="cls()" onblur="res()" ForeColor="Gray"></asp:TextBox>
[解决办法]
(function ($) {
$.fn.watermark = function (options) {
// build main options before element iterationvar 
opts = $.extend({}, $.fn.watermark.defaults, options);
return this.each(function () {
var target = $(this);
function clear() {
if (target.val() == opts.defaultText && target.hasClass(opts.watermarkCss)) {
target.val("").removeClass(opts.watermarkCss);
}
}

function renderDefault() {
if ($.trim(target.val()) === '') {
target.val(opts.defaultText).addClass(opts.watermarkCss);
}
}

// Bind the related event handlers


target.focus(clear);
target.blur(renderDefault);
target.change(renderDefault);
renderDefault();
});

};

热点排行