InputEvent: inputType 屬性

Baseline 廣泛可用 *

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

* 此特性的某些部分可能存在不同級別的支援。

inputTypeInputEvent 介面的一個只讀屬性,用於返回對可編輯內容所做的更改的型別。可能的更改包括插入、刪除和格式化文字等。

一個包含所作輸入型別的字串。有許多可能的值,例如 insertTextdeleteContentBackwardinsertFromPasteformatBold。有關可用輸入型別的完整列表,請參閱 Input Events Level 2 規範的 Attributes 部分

示例

此示例記錄了在可編輯的 <div> 元素上發生的 input 事件inputType

HTML

html
<p id="log">Input type:</p>
<div contenteditable="true" class="sample-text">
  <p>
    Some sample text. Try inserting line breaks, or deleting text in different
    ways, or pasting different content in.
  </p>
  <hr />
  <ul>
    <li>A sample</li>
    <li>bulleted</li>
    <li>list.</li>
  </ul>
  <p>Another paragraph.</p>
</div>

CSS

css
.sample-text {
  margin: 20px;
  padding: 20px;
  border: 2px dashed red;
}

JavaScript

js
const log = document.getElementById("log");
const editable = document.querySelector("div[contenteditable]");
editable.addEventListener("input", logInputType);

function logInputType(event) {
  log.textContent = `Input type: ${event.inputType}`;
}

結果

嘗試編輯 <div> 中的文字,看看會發生什麼。

注意:另請參閱 Masayuki Nakano 的 InputEvent 測試套件,以獲取更詳細的示例。

規範

規範
UI 事件
# dom-inputevent-inputtype

瀏覽器相容性