我正在尝试创建与 https://login.microsoftonline.com/ 中类似的登录名.我想在字段中显示描述“someone@example.com”和“密码”。
我尝试使用 txReplaceFormPassword.js ( http://snipplr.com/view/29555/ ) 脚本来动态替换字段,但它返回的是 html 文本而不是实际字段。
<head>
<script src="@Url.Content("~/Scripts/txReplaceFormPassword.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.pwdfield').txReplaceFormPassword({
show_text: 'Password'
});
});
</script>
</head>
<div class="pwdfield">
@Html.PasswordFor(model => model.Password, new {@class = "k-textbox", style = "width:300px"})
@Html.ValidationMessageFor(model => model.Password)
</div>
我在浏览器中得到以下输出:
请告诉我如何在密码/用户名字段中获取类似于上面两个链接的描述。
谢谢。
最佳答案
我想这就是你想要的:
<input type="text" placeholder="someone@example.com" /><br />
<input type="password" placeholder="Password" />
据我所知,您不需要为此使用 js 或 jQuery。只需将 placeholder=""
设置为您要在字段中显示的文本即可。
看看 this link .
编辑
然后使用下面的 jQuery(在 ie 7 上测试过):
(function($){
var placeholderIsSupported = ('placeholder' in document.createElement('input'));
$.fn.emulatePlaceholder = function(){
if(!placeholderIsSupported){
this.each(function(index, element){
var handle = $(element);
var placeholder = handle.attr('placeholder');
if(handle.val() == ''){
handle.val(placeholder);
}
handle.blur(function(e){
var handle = $(this);
if(handle.val() == ''){
handle.val(placeholder);
}
});
handle.focus(function(e){
var handle = $(this);
if(handle.val() == placeholder){
handle.val('');
}
});
});
}
};
})(jQuery);
用法:
$('input').emulatePlaceholder();
jsFiddle例子
关于javascript - 在用户名和密码字段中显示说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252048/