As of Chrome 53, how to add text as if a trusted textInput event was dispatched?

前端 未结 1 897
暗喜
暗喜 2020-12-19 21:59

As of Chrome 53, untrusted events no longer invoke the default action. https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted

Before Chrome 53, this JavaS

相关标签:
1条回答
  • 2020-12-19 22:46

    Switch to document.execCommand that works in any text element as well as any element with contenteditable="true" and generates a trusted "input" event. The text is inserted at the caret position (replacing selection if any) just as if it was typed by a user. The only drawback compared to TextEvent event is that "input" event doesn't contain the inserted text.

    document.execCommand('insertText', false, String.fromCharCode(8253));
    document.execCommand('insertHTML', false, '‽'); // the same
    

    https://jsfiddle.net/2nfhrj1j/22/

    0 讨论(0)
提交回复
热议问题