1

我正在尝试创建与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>

我在浏览器中得到以下输出:

密码字段

请让我知道如何在密码/用户名字段中获得类似于上面两个链接的描述。

谢谢。

4

1 回答 1

2

我认为这就是你想要的:

<input type="text" placeholder="someone@example.com" /><br />
<input type="password" placeholder="Password" />

据我所知,您不需要为此使用 js 或 jQuery。只需将 设置为placeholder=""要在字段中显示的文本。

看看这个链接

编辑

然后使用以下 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示例

于 2013-04-27T12:26:47.560 回答