我希望默认值为0,如果输入的值大于0,使用:valid
重新着色。我做错了什么吗?是谷歌浏览器出了问题还是HTML5规范有误?我想在不使用JS的情况下解决这个问题。
:valid {
background: green
}
<p>This should not be green, but it is:</p>
<input max="100" min="0" pattern="^[1-9]\d*$" type="number" value="0" />
<p>Works perfectly, if the type is changed to text:</p>
<input pattern="^[1-9]\d*$" type="text" value="0" />
number
不是适用于pattern
属性的输入类型之一。https://html.spec.whatwg.org/multipage/input.html#concept-input-apply - chharvey模式(pattern)
作为一个不适用的属性。 - chharvey