InputEvent: inputType 屬性
Baseline 廣泛可用 *
inputType 是 介面的一個只讀屬性,用於返回對可編輯內容所做的更改的型別。可能的更改包括插入、刪除和格式化文字等。InputEvent
值
一個包含所作輸入型別的字串。有許多可能的值,例如 insertText、deleteContentBackward、insertFromPaste 和 formatBold。有關可用輸入型別的完整列表,請參閱 Input Events Level 2 規範的 Attributes 部分。
示例
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 |
瀏覽器相容性
載入中…