InputEvent.inputType

InputEvent 接口中的只读属性 inputType 返回对可编辑内容所做更改的类型。可能的更改包括插入、删除和格式化文本。

语法

var string = inputEvent.inputType;

返回值

一个 DOMString 对象,包含所做输入的类型。有许多可能的值,例如insertTextdeleteContentBackwardinsertFromPasteformatBold。有关可用输入类型的完整列表,请参阅 Input Events Level 1 规范的属性部分

示例

此实例记录 input events 的 inputType,在一个可编辑的 <div> 中。

HTML

<p id="log">Input type: </p>
<div contenteditable="true" style="margin: 20px;padding: 20px;border:2px dashed red;">
  <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>

JavaScript

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 Events
inputType
Working Draft

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
inputType
Experimental
Chrome Full support 60Edge Full support 79Firefox Full support 66IE No support NoOpera Full support 47Safari Full support 10.1WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 66Opera Android Full support 44Safari iOS Full support 10.3Samsung Internet Android Full support 8.0
insertFromPasteAsQuotation input type
Experimental
Chrome No support NoEdge No support NoFirefox Full support 67IE No support NoOpera No support NoSafari ? WebView Android No support NoChrome Android No support NoFirefox Android Full support 67Opera Android No support NoSafari iOS ? Samsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.