chrome浏览器自动填充表单的黄色背景高亮(#FAFFBD)一直困扰着我,我之前没想着理它,可是最近一个登陆框,需要用到图标,于是我草率的直接设置在input元素里面,结果问题就来了,很难看很难看,因此还是总结一下。

这个问题,在2008年的时候就已经存在了,隔了好几年了,在chromium上面可以找到 Issue 46543,但是官方好像没有理这个问题,英文没怎么看懂,谁理解的,可以给大家分享一下。

思路一: 打补丁

Webkit内核的浏览器有一个-webkit-autofill私有属性,

通过审查元素可以看到这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对其赋予以下样式:

1
2
3
4
5
input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 153, 204);">rgb(250, 255, 189); /* #FAFFBD; */
background-image: none;
color: rgb(0, 0, 0);
}

因此我们就会想到覆盖这个样式,代码如下,但是依然不能覆盖原有的背景、字体颜色。需要注意的是:加 !important 依然不能覆盖原有的背景、字体颜色,除了chrome默认定义的background-colorbackground-imagecolor不能用 !important 提升其优先级以外,其他的属性均可使用!important提升其优先级。

1
2
3
4
5
6
7
8
9
10
11
12
input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline;">#FFFFFF;
background-image: none;
color: #333;
/* -webkit-text-fill-color: red; //这个私有属性是有效的 */
}
input:-webkit-autofill:hover {
/* style code */
}
input:-webkit-autofill:focus {
/* style code */
}

情景一:input文本框是纯色背景的

解决办法:

1
2
3
4
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0px 1000px white inset;
-webkit-text-fill-color: #333;
}

情景二:input文本框是使用图片背景的

解决办法:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0)
{
var _interval = window.setInterval(function () {
var autofills = $('input:-webkit-autofill');
if (autofills.length > 0) {
window.clearInterval(_interval); // 停止轮询
autofills.each(function() {
var clone = $(this).clone(true, true);
$(this).after(clone).remove();
});
}
}, 20);
}

下面的js不是太靠谱

1
2
3
4
5
6
7
8
if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
$(window).load(function(){
$('input:-webkit-autofill').each(function(){
var clone = $(this).clone(true, true);
$(this).after(clone).remove();
});
});
}

思路二: 关闭浏览器自带填充表单功能

设置表单属性 autocomplete="off/on" 关闭自动填充表单,自己实现记住密码

1
2
3
4
<!-- 对整个表单设置 -->
<form autocomplete="off" method=".." action="..">
<!-- 或对单一元素设置 -->
<input type="text" name="textboxname" autocomplete="off">

网上大部分文章是使用Cookie实现记住用户名、密码。不管是在前端,还是后端都可以实现。本文不对Cookie存储展开讨论。可自行谷歌

原文:http://zcoder.cn/2015/01/14/front-end/chrome-autofill/

Chrome 自动填充的表单是淡黄色的背景怎么办!的更多相关文章

  1. Chrome 自动填充的表单是淡黄色的背景

    Chrome 自动填充的表单是淡黄色的背景解决方案; input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #fff inset; - ...

  2. Chrome 自动填充的表单是淡黄色的背景,有方法自定义吗

    input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset; }

  3. 结合Bootbox将后台JSON数据填充Form表单

    本文介绍了如何结合Bootbox将后台JSON数据填充到Form表单中,同时也介绍了一些需要使用的知识的学习途径,并附上了参考文档地址与学习网址,对此感兴趣的伙伴可以直接访问学习.为了方便介绍,使用了 ...

  4. jquery自动将form表单封装成json的具体实现

    前端页面:<span style="font-size:14px;"> <form action="" method="post&q ...

  5. Java 创建、填充PDF表单域

    表单域,可以按用途分为多种不同的类型,常见的有文本框.多行文本框.密码框.隐藏域.复选框.单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据.下面的示例中,将分享通过Java编程在PDF中添加 ...

  6. FXForms,自动生成iOS表单

    1.简介 FXForms是一个简单的表单提交框架,他的作者是鼎鼎大名的 Nick Lockwood,你也许听说过他的其他的一些框架,比如 iCarousel. 为什么使用FxForms? 表单处理简单 ...

  7. form表单中只有一个input时,按回车键后表单自动提交(form表单的一个小坑)

    form中只有一个input按回车键表单会自动提交 在一个form表单中,若只有一个input,按回车键表单会自动提交,但是当表单中存在多个input时,按回车键不会执行任何操作,这是form表单的一 ...

  8. Select下拉列表选择自动提交form表单数据

    HTML代码: <form action='__CONTROLLER__/index' method="get" id="myform"> < ...

  9. puppeteer 填充基础表单

    main.js const pptr = require("puppeteer"); const gotoUrl = "http://127.0.0.1:5500/ind ...

随机推荐

  1. hdu1029

    #include<iostream>#include<string.h>using namespace std;int main(){ int n,i; int t; int ...

  2. centos6 + tomcat+ jdk配置步骤

    1. 获取tomcat, jdk安装文件 mkdir /media/smbdirmount -o username=pas,password=111111 //109.110.100.50/pas / ...

  3. DB2中coalesce函数的应用

    在ETL项目中经常会碰到这样的一种情况: 目标表中的某列来源于不同的源数据表A,B,C.如果在A中没有有效的数据则从B中取,如果B中没有则从C中取,如果C中也没有则设置为空值. 遇到这样的情况可能,有 ...

  4. c++实现快速排序详细分析

    快速排序坑挺多的,今天有空记录一下自己的实现,并加上详细的注释和举例 #include<iostream> using namespace std; int partion(int num ...

  5. sqlserver2012评估期已过问题处理

    于之前安装sqlserver2012忘记输入序列号,现在出现评估期已过的问题,网上忙活半天,才解决,发现网上叙述都很凌乱,而且只有大意,新手很难操作,所以把我操作的过程分享给大家 步骤阅读   百度经 ...

  6. hbase自带mapreduce计数表行数功能

    $HBASE_HOME/bin/hbase org.apache.hadoop.hbase.mapreduce.RowCounter ‘tablename’ mapreduce来计数,很快的!!!

  7. 部分服务器使用phpExcel会报错

    其中一个错误提示是:Fatal error: 'break' not in the 'loop' or 'switch' context in /var/www/htdocs/hanya/ThinkP ...

  8. Faster-R-CNN编译使用及相应问题解决

    1.首先opencv是需要安装的,我用的ubuntu14.04,opencv3.0,具体安装教程可以参考网上很多,不想多提. 2.安装几个依赖包:cython,python-opencv和easydi ...

  9. ZZNU 1993: cots' friends

    题目描述 cot 最近非常喜欢数字, 喜欢到了什么程度呢, 已经走火入魔了.... cot把每个朋友编上一个序号,然后遇到谁就叫"XX号",对此,他的朋友们一致认为cot" ...

  10. 详细解析Java中抽象类和接口的区别(转)

    转自:http://dev.yesky.com/436/7581936.shtml 在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机 ...