这是自动完成的每个浏览器,除了Chrome。

我猜我必须专门针对Chrome。

有解决方案吗?

如果不是用CSS,那么用jQuery?


当前回答

2018 > JQUERY v3.3解决方案: 工作全局为所有输入,文本区域与占位符。

 $(function(){
     $('input, textarea').on('focus', function(){
        if($(this).attr('placeholder')){
           window.oldph = $(this).attr('placeholder');
            $(this).attr('placeholder', ' ');
        };
     });

     $('input, textarea').on('blur', function(){
       if($(this).attr('placeholder')){
            $(this).attr('placeholder', window.oldph);
         };
     }); 
});

其他回答

$("input[placeholder]").each(function () {
    $(this).attr("data-placeholder", this.placeholder);

    $(this).bind("focus", function () {
        this.placeholder = '';
    });
    $(this).bind("blur", function () {
        this.placeholder = $(this).attr("data-placeholder");
    });
});
$("input[placeholder]").focusin(function () {
    $(this).data('place-holder-text', $(this).attr('placeholder')).attr('placeholder', '');
})
.focusout(function () {
    $(this).attr('placeholder', $(this).data('place-holder-text'));
});

2018 > JQUERY v3.3解决方案: 工作全局为所有输入,文本区域与占位符。

 $(function(){
     $('input, textarea').on('focus', function(){
        if($(this).attr('placeholder')){
           window.oldph = $(this).attr('placeholder');
            $(this).attr('placeholder', ' ');
        };
     });

     $('input, textarea').on('blur', function(){
       if($(this).attr('placeholder')){
            $(this).attr('placeholder', window.oldph);
         };
     }); 
});

这是一个css的解决方案(目前,只适用于WebKit):

input:focus::-webkit-input-placeholder {
    opacity: 0;
}

有时你需要特异性来确保你的风格应用于最强的因素id,谢谢@Rob Fletcher的精彩回答,在我们公司我们使用过

因此,请考虑添加带有应用程序容器id前缀的样式

#app input:focus::-webkit-input-placeholder, #app textarea:focus::-webkit-input-placeholder { 颜色:# FFFFFF; } #app input:focus:-moz-placeholder, #app textarea:focus:-moz-placeholder { 颜色:# FFFFFF; }