Removing input background colour for Chrome autocomplete?

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

    I have developed another solution using JavaScript without JQuery. If you find this useful or decide to re-post my solution, I only ask that you include my name. Enjoy. – Daniel Fairweather

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

    This code is based on the fact that Google Chrome removes the Webkit style as soon as additional text is entered. Simply changing the input field value does not suffice, Chrome wants an event. By focusing on each input field (text, password), we can send a keyboard event (the letter 'a') and then set the text value to it's previous state (the auto-filled text). Keep in mind that this code will run in every browser and will check every input field within the webpage, adjust it accordingly to your needs.

提交回复
热议问题