4

我正在使用 jQuery 而不是 HTML5 占位符属性

<input type="text" name="email" value="Email" onfocus="if (this.value == 'Email') { this.value = ''; }" onblur="if (this.value == '') { this.value = 'Email'; }" onclick="if (this.value == 'Email') { this.value = ''; }"  />

这很好用,type="text"type="password"它只显示*

我将如何使用占位符作为密码字段?在 IE8 中需要

提前感谢您的回答...

4

5 回答 5

15

我创建了一个普通输入,然后focus将其切换到密码字段,如果值为空 on blur,则将其切换回普通输入,否则将其保留为密码字段。

这是一个有效的 JSFiddle:http: //jsfiddle.net/q8ajJ/

的HTML

<!-- Style = display none for people who dont have javascript -->
<input type="text" name="fake_pass" id="fake_pass" value="Enter Password:" style="display:none"/>
<input type="password" name="real_pass" id="real_pass"/>​

Javascript (jQuery)

// On DOM ready, hide the real password
$('#real_pass').hide();

// Show the fake pass (because JS is enabled)
$('#fake_pass').show();

// On focus of the fake password field
$('#fake_pass').focus(function(){
    $(this).hide(); //  hide the fake password input text
    $('#real_pass').show().focus(); // and show the real password input password
});

// On blur of the real pass
$('#real_pass').blur(function(){
    if($(this).val() == ""){ // if the value is empty, 
        $(this).hide(); // hide the real password field
        $('#fake_pass').show(); // show the fake password
    }
    // otherwise, a password has been entered,
    // so do nothing (leave the real password showing)
});

​</p>

于 2012-11-16T11:06:59.690 回答
6

是另一个肮脏的技巧 - 您可以使用 JS 在焦点和模糊上创建和显示/隐藏它labelposition: absolute

据我所知,它适用于任何浏览器。此外,验证表单提交更简单。

HTML

<div class="wrapper">
    <input class="withPlaceholder" type="password" id="pass" />
    <label class="placeholder" for="pass">Password</label>
</div>
<br />
<div class="wrapper">
    <input class="withPlaceholder" type="password" id="pass2" />
    <label class="placeholder" for="pass2">Password2</label>
</div>

CSS

div.wrapper {position: relative}
label.placeholder {position: absolute; color: #CCC; left: 2px; top: 0} 

JS

$("input.withPlaceholder").on({
    focus: function() {
        $("label[for=" + $(this).prop("id") + "]").hide();
    },
    blur: function() {
        if ($(this).val().length == 0) {
            $("label[for=" + $(this).prop("id") + "]").show();
        }
    }
});
于 2012-11-16T12:34:13.567 回答
4

根据 JustAnil 的回答,并寻找一种方法来避免必须为每个密码字段手动添加每个对应项,我想出了 JustAnil 解决方案的这种改编:

function ManagePasswords(root) {
    root.find('input[type=password][placeholder]').each(function (index, current) {
        var $current = $(current),
            $fake = $current.clone().attr('type', 'text');

        $fake.val($fake.attr('placeholder'));

        $fake.insertBefore($current);
        $current.hide();

        $fake.focusin(function () {
            $(this).hide();
            $(this).next().show().focus();
        });

        $current.blur(function () {
            if ($(this).val() == '') {
                $(this).hide();
                $(this).prev().show();
            }
        });
    });
}

然后你所要做的就是打电话

ManagePasswords($('#rootElement'));

希望这可以帮助!

于 2013-06-12T11:54:49.003 回答
1

input为占位符添加另一个元素可能会在form验证和提交期间产生问题。我以不同的方式解决了这个问题。

的HTML

<form id="test">
    <input type="text" id="name" name="name" placeholder="Name"  />
    <input type="password" id="password" name="password" placeholder="Password" />
</form>

jQuery 函数

function placeholder(form) {
    form.wrapInner('<ul style="list-style: none; list-style-type:none; padding:0; margin: 0;">');
    form.find('input[placeholder]').each(function (index, current) {
        $(current).css('padding', 0).wrap('<li style="position: relative; list-style: none; list-style-type:none; padding:0; margin: 0;">');            
        var height = $(current).parent('li').height();
        var $current = $(current),
            $placeholder = $('<div class="placeholder">'+$current.attr('placeholder')+'</div>');
        $placeholder.css({'color': '#AAA', 'position':'absolute', 'top': 0, 'left': 0, 'line-height': height+'px', 'margin': '0 0 0 5px', 'border': '0 none', 'outline': '0 none', 'padding': 0});
        $placeholder.insertAfter($current);
        $current.removeAttr('placeholder');
        $placeholder.click(function(){
            $current.focus()
        });
        $current.keypress(function(){
            if($(this).val().length >= 0) {
                $placeholder.hide();
            } 
        });
        $current.blur(function(){
            if($(this).val().length == 0) {
                $placeholder.show();
            } else {
                $placeholder.hide();
            }
        });                 
    });         
}

现在只需为您的表单调用函数。

placeholder($("#test"));

这适用于所有类型的带占位符的输入字段,包括type="password"在 IE8、IE9、IE10、Google Chrome、FireFox、Safari 和 Opera 中测试。

于 2013-07-11T17:04:40.830 回答
1

使用此代码,jQuery 将完成剩下的工作......

html标签替换

<!--[if IE 8]><html class="ie8"><![endif]-->
<!--[if gt IE 8]><html><![endif]-->

jQuery 天...

$(document).ready(function(){

$('.ie8 [placeholder][type="password"]').each(function(){
        $(this).wrap('<div style="position: relative;"></div>');
        $('<span style=" position: absolute;top: 5px;left:14px;" class="ie8Lbls">'+$(this).attr('placeholder')+'</span>').insertAfter($(this));
        $(this).attr('placeholder','');
        if($(this).val() == "") {$(this).parent().find('.ie8Lbls').show();}
    }).on('focus',function(){
        $(this).parent().find('.ie8Lbls').hide();
    }).on('blur',function(){
        if($(this).val() == "") {$(this).parent().find('.ie8Lbls').show();}
    });
    $(document).on('click','.ie8Lbls',function(){
        $(this).parent().find('input').focus();
    });

});

更改新占位符外观的样式

<style>
.ie8Lbls {
    font-size:10px;
}
</style>
于 2014-06-19T00:14:03.637 回答