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

瀏覽器相容性