这可能是一个有点菜鸟的问题,所以提前道歉但是...HTML5 规范是否定义了用户代理应该如何响应 HTML5 必需的属性(或者由他们决定)?换句话说,是否有任何具体的规则来定义用户代理的行为方式(例如“用户应该被提醒必填字段的存在”;“当用户提交表单时,必填的控件应该被验证并在它们不具有值的情况下显示错误消息”;等)
HTML5 规范似乎提供了一些模糊的指南,但没有具体内容。
最佳答案
规范指出,当一个 required
element有一个空字符串的值:
the element is suffering from being missing
如果我们再看看 section on "constraints" (这解释了“缺失”的含义),它告诉我们:
An element can have a custom validity error message defined. Initially, an element must have its custom validity error message set to the empty string. When its value is not the empty string, the element is suffering from a custom error. It can be set using the
setCustomValidity()
method. The user agent should use the custom validity error message when alerting the user to the problem with the control.
将如何显示此消息的确切实现留给浏览器是有意义的,因此它们可以使其适应当前的样式和用户体验。
有浏览器如何以不同方式处理此问题的示例(屏幕截图)here ,以及浏览器兼容性信息:
关于html - 用户代理应该如何处理 HTML5 必需的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12387977/