在我的表单中,我想使用新的HTML表单类型,例如<input type="url" />(关于类型的更多信息在这里)。
问题是Chrome想要超级有用,并验证这些元素为我,除了它在这方面很糟糕。如果它没有通过内置验证,除了元素获得焦点之外,没有其他消息或指示。我用“http://”预填充URL元素,所以我自己的自定义验证只是将这些值视为空字符串,但Chrome拒绝了这一点。如果我能改变它的验证规则,那也可以。
我知道我可以回到使用type="text",但我想使用这些新类型提供的很好的增强(例如:它自动切换到移动设备上的自定义键盘布局):
有没有办法关闭或自定义自动验证?
您可以将http://作为占位符文本放入,而不是试图绕过浏览器的验证。这是来自你链接的页面:
Placeholder Text
The first improvement HTML5 brings to web forms is the ability to set placeholder text in an input field. Placeholder text is displayed inside the input field as long as the field is empty and not focused. As soon as you click on (or tab to) the input field, the placeholder text disappears.
You’ve probably seen placeholder text before. For example, Mozilla Firefox 3.5 now includes placeholder text in the location bar that reads “Search Bookmarks and History”:
When you click on (or tab to) the location bar, the placeholder text disappears:
Ironically, Firefox 3.5 does not support adding placeholder text to your own web forms. C’est la vie.
Placeholder Support
IE FIREFOX SAFARI CHROME OPERA IPHONE ANDROID
· 3.7+ 4.0+ 4.0+ · · ·
Here’s how you can include placeholder text in your own web forms:
<form>
<input name="q" placeholder="Search Bookmarks and History">
<input type="submit" value="Search">
</form>
Browsers that don’t support the placeholder attribute will simply ignore it. No harm, no foul. See whether your browser supports placeholder text.
它不会完全相同,因为它不会为用户提供“起点”,但它至少已经达到了一半。