有没有一种方法可以使输入值大写而不使占位符大写?
看起来我只能得到其中之一。我更喜欢只使用CSS(JS是最后的手段)来干净地完成这个任务。
每个浏览器引擎都有不同的实现方式来控制placeholder
的样式。使用以下内容:
input {
text-transform: uppercase;
}
::-webkit-input-placeholder { /* WebKit browsers */
text-transform: none;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
text-transform: none;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
text-transform: none;
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
text-transform: none;
}
::placeholder { /* Recent browsers */
text-transform: none;
}
不用说,在能够处理占位符的浏览器中才能起作用。(IE9/10+)
input::placeholder { text-transform: none; }
应该被添加到代码中,因为它得到了广泛的支持。详情请参考:https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder - Dem Pilafian我知道你说尽量避免使用JS,但在这种情况下,它可能更有效和更简单(如果你不使用JS,表单提交时的值将不会大写),所以这可能是更好的选择,不需要额外的CSS:
<input oninput="this.value = this.value.toUpperCase()" placeholder="Enter Drivers License #" />
编辑
有些人抱怨在编辑数值时光标会跳到末尾,所以这个稍微扩展的版本可以解决这个问题。
<input oninput="let p = this.selectionStart; this.value = this.value.toUpperCase();this.setSelectionRange(p, p);" />