Removing input background colour for Chrome autocomplete?

前端 未结 30 2634
失恋的感觉
失恋的感觉 2020-11-22 04:02

On a form I\'m working on, Chrome is auto-filling the email and password fields. This is fine, however, Chrome changes the background colour to a pale yellow colour.

30条回答
  •  野的像风
    2020-11-22 04:21

    Thanks Benjamin!

    The Mootools solution is a little more tricky, as I can't get fields by using $('input:-webkit-autofill'), So what I've used is the following:

    if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
    
      window.addEvent('load', function() {
        setTimeout(clearWebkitBg, 20);
        var elems = getElems();
        for (var i = 0; i < elems.length; i++) {
          $(elems[i]).addEvent('blur', clearWebkitBg);
        }
      });
    }
    function clearWebkitBg () {
      var elems = getElems();
      for (var i = 0; i < elems.length; i++) {
        var oldInput = $(elems[i]);
        var newInput = new Element('input', {
          'name': oldInput.get('name'),
          'id': oldInput.get('id'),
          'type': oldInput.get('type'),
          'class': oldInput.get('class'),
          'value': oldInput.get('value')
        });
        var container = oldInput.getParent();
        oldInput.destroy();
        container.adopt(newInput);
      }
    }
    function getElems() {
      return ['pass', 'login']; // ids
    }
    

提交回复
热议问题