使用 required 属性设置 HTML5 输入字段上的提示样式

发布于 2024-11-01 01:46:50 字数 423 浏览 5 评论 0原文

使用 required 属性时,是否可以设置 HTML5 输入字段上显示的提示的样式。如果您不确定我在说什么,请单击此表单上的“提交”,无需填写任何内容。您应该会看到一个提示弹出窗口。

http://24ways.org/examples /have-a-field-day-with-html5-forms/24ways-form.html

我检查了 CSS 源代码,但看不到任何有关提示的内容。

我发现以重置方式设置 div 元素的样式会影响它的显示方式。但我不知道如何具体针对它。

请注意:我指的不是占位符。

干杯, 托马斯.

Is it possible to style the hint that appears on a HTML5 input field when using the required attribute. If you're not sure what I'm talking about click submit on this form without filling anything in. You should have a hint popup.

http://24ways.org/examples/have-a-field-day-with-html5-forms/24ways-form.html

I've checked the CSS source and couldn't see anything regarding the hint.

I have found that styling the div element in a reset fashion affects how it appears. But I do not know how to target it specifically.

Please note: I am NOT referring to a placeholder.

Cheers,
Thomas.

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

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

发布评论

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

评论(4

风启觞 2024-11-08 01:46:50

为什么你可以使用 div 选择器设置错误气泡的样式,这是 Chrome 11/12 中的一个错误,它应该是 已在新版本中修复。在 Chrome 12 中(也许在 Safari6 中)有一些伪类可以设置错误气泡的样式(::-webkit-validation-bubble 等)。您可以在以下文档中找到完整的 HTML 结构,包括伪元素选择器和一些样式示例。

请注意,这是 HTML5 表单约束验证的 webkit 扩展,并且是非标准的。如果您想在所有支持 HTML5 验证的浏览器中使用某种方式设置错误消息的样式,则必须使用 JavaScript。

其关键原则是,您必须向无效事件添加处理程序(注意:无效事件不会冒泡),然后阻止默认交互。这将消除浏览器的本机错误气泡,并且您可以在所有 HTML5 浏览器中实现自定义样式 UI。

//Note: that we use true for useCapture, because invalid does not bubble
document.addEventListener('invalid', function(e){
    //prevent the browser from showing his error bubble
    e.preventDefault();
    //now you can implement your own validation UI (showError is a Custom method which has to be implemented by you
    showError(e.target, $.prop(e.target, 'validationMessage');
}, true);

上面的代码将为当前表单中的所有无效元素调用 showError 。如果您只想对第一个无效元素执行此操作,您可以执行以下操作:

document.addEventListener('invalid', (function(){
    var isFirst = true;
    return function(e){
        //prevent the browser from showing his error bubble
        e.preventDefault();
        //now you can implement your own validation UI
        if(isFirst){
            showError(e.target, $.prop(e.target, 'validationMessage');
            //set isFirst to false
            isFirst = false;
            //reset isFirst to true, so user can try to submit invalid forms multiple times
            setTimeout(function(){
                isFirst = true;
            }, 9);
        }
    };
})(), true);

如果您在网站上使用 jQuery,我建议使用 webshims 库。 webshims lib 在所有浏览器(包括 IE6)中实现 HTML5 约束验证,并提供一个简单的扩展来生成简单的自定义样式验证消息。 JS 代码如下所示:

$(document).bind('firstinvalid', function(e){
    $.webshims.validityAlert.showFor( e.target ); 
    //prevent browser from showing native validation message 
    return false; 
});

$.webshims.validityAlert.showFor 生成的 HTML 结构如下所示:

<span class="validity-alert" role="alert"> 
    <span class="va-arrow"><span class="va-arrow-box"></span></span> 
    <span class="va-box">Error message of the current field</span> 
</span>

The reason, why you can style the error bubble with a div-selector is a bug in Chrome 11/12, which should be fixed in newer versions. There are some pseudoclasses to style the error bubble in Chrome 12 (and maybee in Safari6) (::-webkit-validation-bubble etc.). You can find the full HTML structure including the pseudoelement selectors and some styling examples in the following document.

Note, that this is a webkit extension to the HTML5 form constraint validation and non-standard. If you want to use a way to style the error message in all HTML5 validation supporting browsers, you have to use JavaScript.

The key principle of this, is that you have to add a handler to the invalid-event (Note: The invalid event does not bubble) and then prevent the default interaction. This will remove the browsers native error bubble and you are able to implement a custom styleable UI in all HTML5 browsers.

//Note: that we use true for useCapture, because invalid does not bubble
document.addEventListener('invalid', function(e){
    //prevent the browser from showing his error bubble
    e.preventDefault();
    //now you can implement your own validation UI (showError is a Custom method which has to be implemented by you
    showError(e.target, $.prop(e.target, 'validationMessage');
}, true);

The code above will call showError for all invalid elements in the current form. If you want to do this only for the first invalid element you can do the following:

document.addEventListener('invalid', (function(){
    var isFirst = true;
    return function(e){
        //prevent the browser from showing his error bubble
        e.preventDefault();
        //now you can implement your own validation UI
        if(isFirst){
            showError(e.target, $.prop(e.target, 'validationMessage');
            //set isFirst to false
            isFirst = false;
            //reset isFirst to true, so user can try to submit invalid forms multiple times
            setTimeout(function(){
                isFirst = true;
            }, 9);
        }
    };
})(), true);

In case you are using jQuery for your site, I would recommend using webshims lib. webshims lib implements the HTML5 constraint validation in all browsers (including IE6) and gives a simple extension for generating a simple custom styleable validation message. The JS code looks like this:

$(document).bind('firstinvalid', function(e){
    $.webshims.validityAlert.showFor( e.target ); 
    //prevent browser from showing native validation message 
    return false; 
});

The HTML-structure generated by $.webshims.validityAlert.showFor looks like this:

<span class="validity-alert" role="alert"> 
    <span class="va-arrow"><span class="va-arrow-box"></span></span> 
    <span class="va-box">Error message of the current field</span> 
</span>
静谧 2024-11-08 01:46:50

您可以使用以下伪选择器在 webkit 中设置验证气泡的样式:

::-webkit-validation-bubble
::-webkit-validation-bubble-top-outer-arrow
::-webkit-validation-bubble-top-inner-arrow
::-webkit-validation-bubble-message

对于 Mozilla 浏览器,还没有办法。如果您想更改文本,Mozilla 确实支持 x-moz-errormessagehttps://developer.mozilla.org/en/HTML/element/input#section_5

you can style the validation bubble in webkit using the following pseudo-selectors:

::-webkit-validation-bubble
::-webkit-validation-bubble-top-outer-arrow
::-webkit-validation-bubble-top-inner-arrow
::-webkit-validation-bubble-message

For Mozilla browsers there isn't a way yet. Mozilla does support x-moz-errormessage if you want to change the text: https://developer.mozilla.org/en/HTML/element/input#section_5

昔梦 2024-11-08 01:46:50

我找到了一种方法来禁用提示的显示。

基本上我发现它们位于 div 元素内,如果我将其添加到顶部,则在我的重置中。

div { display: none; }
body div { display: block; }

然后提示不再出现,但我的其余 div 工作正常。

我还相信提示出现在 HTML 文档标记之外。由于 html div 的样式对提示也没有影响。有趣的东西。

但这仅适用于 Chrome。

I found a way to disable the hints from showing.

Basically I found they're within a div element, in my reset if I add this to the top.

div { display: none; }
body div { display: block; }

Then the hints no longer appear, yet the rest of my divs work fine.

I'm also led to believe that the hints appear outside of the HTML document tag. As styling with html div also has no effect on the hints. Interesting stuff.

This only works in Chrome though.

七堇年 2024-11-08 01:46:50

如果你指的是 Opera 和 Chrome 使用的提示,那么恐怕你不能。

If you're referring to the hints that say Opera and Chrome use then I'm afraid you can't.

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