简体   繁体   English

使用必需属性在HTML5输入字段上设置提示样式

[英]Styling the hint on a HTML5 input field using required attribute

Is it possible to style the hint that appears on a HTML5 input field when using the required attribute. 是否可以在使用required属性时设置HTML5输入字段上显示的提示的样式。 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 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. 我检查了CSS源代码,看不到有关提示的任何内容。

I have found that styling the div element in a reset fashion affects how it appears. 我发现以重置方式设置div元素的样式会影响它的显示方式。 But I do not know how to target it specifically. 但我不知道如何具体针对它。

Please note: I am NOT referring to a placeholder. 请注意:我不是指占位符。

Cheers, Thomas. 干杯,托马斯。

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 version s. 为什么你可以使用div-selector设置错误气泡的原因是Chrome 11/12中的一个错误,应该在较新版本中修复 There are some pseudoclasses to style the error bubble in Chrome 12 (and maybee in Safari6) (::-webkit-validation-bubble etc.). 有一些伪类来设置Chrome 12中的错误气泡(可能还有Safari6)(:: - webkit-validation-bubble等)。 You can find the full HTML structure including the pseudoelement selectors and some styling examples in the following document . 您可以在以下文档中找到完整的HTML结构,包括伪元素选择器和一些样式示例。

Note, that this is a webkit extension to the HTML5 form constraint validation and non-standard. 请注意,这是HTML5表单约束验证和非标准的webkit扩展。 If you want to use a way to style the error message in all HTML5 validation supporting browsers, you have to use JavaScript. 如果要在所有支持HTML5验证的浏览器中使用一种方式设置错误消息样式,则必须使用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. 这个的关键原则是你必须为invalid-event添加一个处理程序(注意:无效事件不会冒泡),然后阻止默认交互。 This will remove the browsers native error bubble and you are able to implement a custom styleable UI in all HTML5 browsers. 这将删除浏览器本机错误气泡,您可以在所有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);

The code above will call showError for all invalid elements in the current form. 上面的代码将为当前表单中的所有无效元素调用showError。 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 . 如果您为您的网站使用jQuery,我建议使用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. webshims lib在所有浏览器(包括IE6)中实现HTML5约束验证,并提供了一个简单的扩展,用于生成简单的自定义样式验证消息。 The JS code looks like this: JS代码如下所示:

$(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: $.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>

you can style the validation bubble in webkit using the following pseudo-selectors: 您可以使用以下伪选择器在webkit中设置验证气泡的样式:

::-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浏览器,还没有办法。 Mozilla does support x-moz-errormessage if you want to change the text: https://developer.mozilla.org/en/HTML/element/input#section_5 如果要更改文本,Mozilla会支持x-moz-errormessagehttps//developer.mozilla.org/en/HTML/element/input#section_5

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元素内,在我的重置中如果我将它添加到顶部。

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

Then the hints no longer appear, yet the rest of my divs work fine. 然后提示不再出现,但其余的div工作正常。

I'm also led to believe that the hints appear outside of the HTML document tag. 我也被认为提示出现在HTML文档标记之外。 As styling with html div also has no effect on the hints. 因为使用html div的样式也不会对提示产生影响。 Interesting stuff. 有趣的东西。

This only works in Chrome though. 这仅适用于Chrome。

如果您指的是Opera和Chrome使用的提示,那么我担心您不能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM