Removing input background colour for Chrome autocomplete?

前端 未结 30 1921
失恋的感觉
失恋的感觉 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:34

    The solution of Daniel Fairweather (Removing input background colour for Chrome autocomplete?) (I would love to upvote his solution, but still need 15 rep) works really good. There is a really huge difference with most upvoted solution : you can keep background images ! But a little modification (just Chrome check)

    And you need to keep in mind, it ONLY works on visible fields !

    So you if you are using $.show() for your form, you need to run this code After show() event

    My full solution (I have a show/hide buttons for login form ):

     if (!self.isLoginVisible()) {
            var container = $("#loginpage");
            container.stop();
            self.isLoginVisible(true);
            if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
    
                var documentForms = document.forms;
                for (i = 0; i < documentForms.length; i++) {
                    for (j = 0; j < documentForms[i].elements.length; j++) {
                        var input = documentForms[i].elements[j];
    
                        if (input.type == "text" || input.type == "password" || input.type == null) {
                            var text = input.value;
                            input.focus();
                            var event = document.createEvent('TextEvent');
                            event.initTextEvent('textInput', true, true, window, 'a');
                            input.dispatchEvent(event);
                            input.value = text;
                            input.blur();
                        }
                    }
                }
            }
    
        } else {
            self.hideLogon();
        }
    

    Sorry again, I would prefer it to be a comment.

    If you want, I can put a link to the site where I used it.

提交回复
热议问题