HTML5 气泡消息

2023-09-30前端开发问题
30

本文介绍了HTML5 气泡消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

输入字段必填有一个新的HTML5属性,当提交表单并且该字段为空时会弹出一个气泡消息.有没有办法用不同的文字弹出同一个气泡?我想将它用于不同的验证(不仅仅是它是否为空).

There is a new HTML5 attribute for input field required which pops up a bubble message when submitting form and the field is empty. Is there any way of popping up the same bubble with different text? I want to use it for different validations (not just if it's empty or not).

这是 Chrome 中此气泡(弹出窗口)的屏幕截图:

Here is a screenshot on this bubble (pop-up) in Chrome:

推荐答案

HTML5 提供了一个名为 setCustomValidity 来实现这一点.此方法是一个低级 API,用于添加新的验证约束和添加新的/更改错误消息.您必须调用您的脚本,该脚本会删除/添加验证消息 - 使用 setCustomValidity -(在DOMready"上和)输入/更改.

HTML5 provides a method called setCustomValidity to achieve exactly this. This method is a low level API to add new validation constraints and to add new/change errormessages. You have to call your script, which removes / adds the validation message - using setCustomValidity - (on "DOMready" and) on input/change.

我写了一个polyfill脚本,它不仅在所有浏览器中实现了那些HTML5表单方法,还添加了一个"customValidity"-helper,这使得使用自定义消息添加自定义验证约束变得更加容易.

I have written a polyfill script, which not only implements those HTML5 form methods in all browsers, but also adds a "customValidity"-helper, which makes it more easy to add custom validation constraints with custom messages.

注意:您无需实现 webshims 库即可使用此脚本.它也可以在没有 webshims 库的情况下工作.(但如果您使用 webshims lib,您将在包括 IE6 在内的所有浏览器中获得约束验证).如果您不想拥有这个跨浏览器,只需使用 有效性帮助脚本.文档在这里.

Note: You don't need to implement webshims lib to make use of this script. It also works without webshims lib. (But if you use webshims lib, you will get constraint validation in all browsers including IE6). If you don't want to have this cross-browser, simply take only the validity-helper script. Documentation is here.

请注意,如果您确实通过设置自定义消息对消息进行了更改,则必须包含一种清除消息的方法,例如

Note that if you do make a change to the message by setting a custom message, you have to include a way to clear the message such as

myFormElement.setCustomValidity('');

myFormElement.setCustomValidity('');

如果您不清除消息,表单元素将被视为无效,表单将不会提交.

if you don't clear the message, the form element will be seen as invalid and the form will not submit.

这篇关于HTML5 气泡消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125