去掉 谷歌瀏覽器賬號(hào)/密碼自動(dòng)填充后input的屎黃色

https://segmentfault.com/q/1010000000671971

chrome瀏覽器自動(dòng)填充表單的黃色背景高亮(#FAFFBD)一直困擾著我,我之前沒想著理它,可是最近一個(gè)登陸框,需要用到圖標(biāo),于是我草率的直接設(shè)置在input元素里面,結(jié)果問題就來了,很難看很難看,因此還是總結(jié)一下。

這個(gè)問題,在2008年的時(shí)候就已經(jīng)存在了,隔了好幾年了,在chromium上面可以找到 Issue 46543,但是官方好像沒有理這個(gè)問題,英文沒怎么看懂,誰理解的,可以給大家分享一下。

思路一: 打補(bǔ)丁

Webkit內(nèi)核的瀏覽器有一個(gè)-webkit-autofill私有屬性,

通過審查元素可以看到這是由于chrome會(huì)默認(rèn)給自動(dòng)填充的input表單加上input:-webkit-autofill私有屬性,然后對(duì)其賦予以下樣式:

input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
  background-color: rgb(250, 255, 189); /* #FAFFBD; */
  background-image: none;
  color: rgb(0, 0, 0);
}

因此我們就會(huì)想到覆蓋這個(gè)樣式,代碼如下,但是依然不能覆蓋原有的背景、字體顏色。需要注意的是:加 !important 依然不能覆蓋原有的背景、字體顏色,除了chrome默認(rèn)定義的background-color,background-image,color不能用 !important 提升其優(yōu)先級(jí)以外,其他的屬性均可使用!important提升其優(yōu)先級(jí)。

input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
  background-color: #FFFFFF;
  background-image: none;
  color: #333;
  /* -webkit-text-fill-color: red; //這個(gè)私有屬性是有效的 */
}
input:-webkit-autofill:hover {
  /* style code */
}
input:-webkit-autofill:focus {
  /* style code */
}

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

解決辦法:

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

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

解決辦法:

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不是太靠譜

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();
    });
  });
}

思路二: 關(guān)閉瀏覽器自帶填充表單功能

設(shè)置表單屬性 autocomplete="off/on" 關(guān)閉自動(dòng)填充表單,自己實(shí)現(xiàn)記住密碼

<!-- 對(duì)整個(gè)表單設(shè)置 -->
<form autocomplete="off" method=".." action="..">
<!-- 或?qū)我辉卦O(shè)置 -->
<input type="text" name="textboxname" autocomplete="off">

網(wǎng)上大部分文章是使用Cookie實(shí)現(xiàn)記住用戶名、密碼。不管是在前端,還是后端都可以實(shí)現(xiàn)。本文不對(duì)Cookie存儲(chǔ)展開討論。可自行谷歌

其他

stackoverflow.com上面也有類似的回答
Google Chrome form autofill and its yellow background
Override browser form-filling and input highlighting with HTML/CSS

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容