如何阻止 Chrome 使我网站的输入框变黄?

发布于 2024-07-06 23:39:04 字数 418 浏览 8 评论 0原文

在表单提交、验证后的其他文本和视觉辅助工具中,我将输入框涂成红色,以表示需要注意的交互区域。

在 Chrome 上(以及 Google 工具栏用户),自动填充功能会将我的输入表单重新着色为黄色。 这是一个复杂的问题:我希望在我的表单上允许自动完成,因为它可以加快用户登录速度。我将检查在触发错误时关闭自动完成属性的能力,但这是一个复杂的过程一些编码,用于以编程方式关闭页面上单个受影响输入的自动完成功能。 简而言之,这将是一个令人头疼的问题。

那么,为了避免这个问题,有没有更简单的方法来阻止 Chrome 重新为输入框着色?

[编辑] 我尝试了下面的!重要建议,但没有效果。 我还没有检查 Google 工具栏以查看 !important 属性是否适用。

据我所知,除了使用自动完成属性(它似乎确实有效)之外,没有任何其他方法。

Among other text and visual aids on a form submission, post-validation, I'm coloring my input boxes red to signify the interactive area needing attention.

On Chrome (and for Google Toolbar users) the auto-fill feature re-colors my input forms yellow. Here's the complex issue: I want auto-complete allowed on my forms, as it speeds users logging in. I am going to check into the ability to turn the autocomplete attribute to off if/when there's an error triggered, but it is a complex bit of coding to programmatically turn off the auto-complete for the single affected input on a page. This, to put it simply, would be a major headache.

So to try to avoid that issue, is there any simpler method of stopping Chrome from re-coloring the input boxes?

[edit] I tried the !important suggestion below and it had no effect. I have not yet checked Google Toolbar to see if the !important attribute would work for that.

As far as I can tell, there isn't any means other than using the autocomplete attribute (which does appear to work).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

舂唻埖巳落 2024-07-13 23:39:05

这正是您所寻找的!

// Just change "red" to any color
input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px red inset;
}

this is exactly what your looking for!

// Just change "red" to any color
input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px red inset;
}
单挑你×的.吻 2024-07-13 23:39:05

通过使用一些 jQuery,您可以删除 Chrome 的样式,同时保持自动完成功能完整。 我在这里写了一篇关于它的简短文章:
http://www.benjaminmiles.com/2010 /11/22/fixing-google-chromes-yellow-autocomplete-styles-with-jquery/

if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
$(window).load(function(){
    $('input:-webkit-autofill').each(function(){
        var text = $(this).val();
        var name = $(this).attr('name');
        $(this).after(this.outerHTML).remove();
        $('input[name=' + name + ']').val(text);
    });
});}

By using a bit of jQuery you can remove Chrome's styling while keeping the autocomplete functionality intact. I wrote a short post about it here:
http://www.benjaminmiles.com/2010/11/22/fixing-google-chromes-yellow-autocomplete-styles-with-jquery/

if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
$(window).load(function(){
    $('input:-webkit-autofill').each(function(){
        var text = $(this).val();
        var name = $(this).attr('name');
        $(this).after(this.outerHTML).remove();
        $('input[name=' + name + ']').val(text);
    });
});}
月竹挽风 2024-07-13 23:39:05

要删除所有字段的边框,您可以使用以下命令:

*:focus {outline:none; 要

删除选择字段的边框,只需将此类应用于所需的输入字段:

.nohighlight:focus { Outline:none;

您当然也可以根据需要更改边框:

.changeborder:focus {outline:Blue Solid 4px; }

(来自比尔·贝克尔曼:使用 CSS 覆盖活动字段周围 Chrome 的自动边框

To remove the border for all fields you can use the following:

*:focus { outline:none; }

To remove the border for select fields just apply this class to the input fields you want:

.nohighlight:focus { outline:none; }

You can of course change the border as you desire as well:

.changeborder:focus { outline:Blue Solid 4px; }

(From Bill Beckelman: Override Chrome's Automatic Border Around Active Fields Using CSS)

水溶 2024-07-13 23:39:05

是的,这将是一个令人头疼的问题,在我看来,这不值得回报。 也许你可以稍微调整一下你的 UI 策略,而不是把盒子染成红色,你可以把边框染成红色,或者在它旁边放一个小的红色胶带(比如 gmails 的“加载”胶带),当盒子放进去时它就会消失。重点。

Yes, it would be a major headache, which in my opinion isnt worth the return. Maybe you could tweak your UI strategy a bit, and instead of coloring the box red, you could color the borders red, or put a small red tape beside it (like the gmails "Loading" tape) which fades away when the box is in focus.

救星 2024-07-13 23:39:05

使用 jQuery 简直就是小菜一碟:

if ($.browser.webkit) {
    $("input").attr('autocomplete','off');
}

或者,如果您想更具选择性,请为选择器添加类名。

It's a piece of cake with jQuery:

if ($.browser.webkit) {
    $("input").attr('autocomplete','off');
}

Or if you want to be a bit more selective, add a class name for a selector.

我三岁 2024-07-13 23:39:05

我认为更简单的方法是:

  1. 获取 http://www.quirksmode.org/js/detect。 html
  2. 使用此代码:

    if (BrowserDetect.browser == "Chrome") { 
        jQuery('form').attr('自动完成','关闭'); 
      }; 
      

The simpler way in my opinion is:

  1. Get http://www.quirksmode.org/js/detect.html
  2. Use this code:

    if (BrowserDetect.browser == "Chrome") {
      jQuery('form').attr('autocomplete','off');
    };
    
骑趴 2024-07-13 23:39:05

应用@Benjamin他的解决方案后,我发现按后退按钮仍然会给我黄色突出显示。

我的解决方案是通过应用以下 jQuery javascript 来防止此黄色突出显示再次出现:

<script type="text/javascript">
    $(function() {
        if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
        var intervalId = 0;
            $(window).load(function() {
                intervalId = setInterval(function () { // << somehow  this does the trick!
                    if ($('input:-webkit-autofill').length > 0) {
                        clearInterval(intervalId);
                        $('input:-webkit-autofill').each(function () {
                            var text = $(this).val();
                            var name = $(this).attr('name');
                            $(this).after(this.outerHTML).remove();
                            $('input[name=' + name + ']').val(text);
                        });
                    }
                }, 1);
            });
        }
    });
</script>

希望它可以帮助任何人!

After applying @Benjamin his solution I found out that pressing the back button would still give me the yellow highlight.

My solution somehow to prevent this yellow highlight to come back is by applying the following jQuery javascript:

<script type="text/javascript">
    $(function() {
        if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
        var intervalId = 0;
            $(window).load(function() {
                intervalId = setInterval(function () { // << somehow  this does the trick!
                    if ($('input:-webkit-autofill').length > 0) {
                        clearInterval(intervalId);
                        $('input:-webkit-autofill').each(function () {
                            var text = $(this).val();
                            var name = $(this).attr('name');
                            $(this).after(this.outerHTML).remove();
                            $('input[name=' + name + ']').val(text);
                        });
                    }
                }, 1);
            });
        }
    });
</script>

Hope it helps anyone!!

拔了角的鹿 2024-07-13 23:39:05

这有效。 最重要的是,您可以使用 rgba 值(box-shadow inset hack 不适用于 rgba)。 这是对 @Benjamin 的答案的轻微调整。 我使用 $(document).ready() 而不是 $(window).load()。 它似乎对我来说效果更好——现在 FOUC 少了很多。 我不认为使用 $(document).ready() 有什么缺点。

if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
    $(document).ready(function() {
        $('input:-webkit-autofill').each(function(){
            var text = $(this).val();
            var name = $(this).attr('name');
            $(this).after(this.outerHTML).remove();
            $('input[name=' + name + ']').val(text);
        });
    });
};

This works. Best of all, you can use rgba values (the box-shadow inset hack doesn't work with rgba). This is a slight tweak of @Benjamin's answer. I am using $(document).ready() instead of $(window).load(). It seems to work better for me - now there's much less FOUC. I don't believe there are and disadvantages to using $(document).ready().

if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
    $(document).ready(function() {
        $('input:-webkit-autofill').each(function(){
            var text = $(this).val();
            var name = $(this).attr('name');
            $(this).after(this.outerHTML).remove();
            $('input[name=' + name + ']').val(text);
        });
    });
};
ˉ厌 2024-07-13 23:39:05

对于今天的版本,如果放置在两个登录输入之一中,这也可以工作。 还修复了 40+ 版本和晚期 Firefox 的问题。

<input readonly="readonly" onfocus="this.removeAttribute('readonly');" />

for today's versions, This works too if placed in one of the two login inputs. Also fix the version 40+ and late Firefox issue.

<input readonly="readonly" onfocus="this.removeAttribute('readonly');" />
谷夏 2024-07-13 23:39:05
input:focus { outline:none; }

这对我来说非常有用,但很可能会在您的网站上保持统一,您还希望将其包含在文本区域的 CSS 中:

textarea:focus { outline:none; }

另外,对于大多数人来说,这似乎是显而易见的,但对于初学者来说,您也可以将其设置为这样的颜色:

input:focus { outline:#HEXCOD SOLID 2px ; }
input:focus { outline:none; }

That worked great for me but more than likely to keep things uniform on your site your going to want to also include this in your CSS for textareas:

textarea:focus { outline:none; }

Also it may seem obvious to most but for beginners you can also set it to a color as such:

input:focus { outline:#HEXCOD SOLID 2px ; }
青芜 2024-07-13 23:39:05

如果我没记错的话,样式表中输入背景颜色的重要规则将覆盖 Google 工具栏自动完成 - 大概 Chrome 也是如此。

If I remember correctly, an !important rule in the stylesheet for the background color of the inputs will override the Google toolbar autocomplete - presumably the same would be true of Chrome.

乖乖 2024-07-13 23:39:04

将 CSS 轮廓属性设置为 none。

input[type="text"], input[type="password"], textarea, select { 
    outline: none;
}

如果浏览器也可能添加背景颜色,则可以通过类似的方法来修复

:focus { background-color: #fff; }

Set the CSS outline property to none.

input[type="text"], input[type="password"], textarea, select { 
    outline: none;
}

In cases where the browser may add a background color as well this can be fixed by something like

:focus { background-color: #fff; }
冷情 2024-07-13 23:39:04

我知道在 Firefox 中您可以使用属性 autocomplete="off" 来禁用自动完成功能。 如果这在 Chrome 中有效(尚未测试),您可以在遇到错误时设置此属性。

这可以用于单个元素

<input type="text" name="name" autocomplete="off">

......也可以用于整个表单

<form autocomplete="off" ...>

I know in Firefox you can use the attribute autocomplete="off" to disable the autocomplete functionality. If this works in Chrome (haven't tested), you could set this attribute when an error is encountered.

This can be used for both a single element

<input type="text" name="name" autocomplete="off">

...as well as for an entire form

<form autocomplete="off" ...>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文