HTML autocapitalize 全局属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

autocapitalize 全局属性是一个枚举属性,它控制输入文本是否自动大写,以及如何自动大写。它与

autocapitalize 不影响物理键盘输入时的行为。它会影响其他输入机制的行为,例如移动设备上的虚拟键盘和语音输入。这可以通过自动大写每个句子的第一个字母来帮助用户更快、更轻松地进行数据输入。

可能的值是

noneoff

不要自动大写任何文本。

sentenceson

自动大写每个句子的第一个字符。

words

自动大写每个单词的第一个字符。

characters

自动大写每个字符。

用法说明

  • autocapitalize 可以设置在 <input><textarea> 元素上,以及它们包含的 <form> 元素上。当 autocapitalize 设置在 <form> 元素上时,它会为所有包含的 <input><textarea> 设置自动大写行为,并覆盖包含元素上设置的任何 autocapitalize 值。
  • autocapitalizeurlemailpassword 类型的 <input> 无效,因为这些类型的输入字段从不启用自动大写。
  • 在未指定 autocapitalize 的情况下,浏览器采用的默认行为各不相同。例如:
    • Chrome 和 Safari 默认值为 on/sentences
    • Firefox 默认值为 off/none

示例

HTML

html
<p>Form to test different autocapitalize settings:</p>

<form>
  <div>
    <label for="default">Default: no autocapitalize set</label>
    <input type="text" id="default" name="default" />
  </div>
  <div>
    <label for="off">autocapitalize="off"</label>
    <input type="text" id="off" name="off" autocapitalize="off" />
  </div>
  <div>
    <label for="none">autocapitalize="none"</label>
    <input type="text" id="none" name="none" autocapitalize="none" />
  </div>
  <div>
    <label for="on">autocapitalize="on"</label>
    <input type="text" id="on" name="on" autocapitalize="on" />
  </div>
  <div>
    <label for="sentences">autocapitalize="sentences"</label>
    <input
      type="text"
      id="sentences"
      name="sentences"
      autocapitalize="sentences" />
  </div>
  <div>
    <label for="words">autocapitalize="words"</label>
    <input type="text" id="words" name="words" autocapitalize="words" />
  </div>
  <div>
    <label for="characters">autocapitalize="characters"</label>
    <input
      type="text"
      id="characters"
      name="characters"
      autocapitalize="characters" />
  </div>
  <div>
    <label for="characters-ta">autocapitalize="characters" on textarea</label>
    <textarea
      type="text"
      id="characters-ta"
      name="characters-ta"
      autocapitalize="characters">
    </textarea>
  </div>
</form>

<hr />

<p contenteditable autocapitalize="characters">
  This content is editable and has autocapitalize="characters" set on it
</p>

结果

使用虚拟键盘或语音输入测试每个输入字段的效果(键盘输入无效)。

规范

规范
HTML
# attr-autocapitalize

浏览器兼容性