How can you catch a contentEditable paste event?

北城余情 提交于 2019-12-05 05:10:53

You can't access the content's that will be inserted.

What you can do is add an event listener that runs some cleanup code on Ctrl+V (with a timeout, so it sees the pasted text)

the onpaste event works fine, at least u can reject user insert behavior

someElement.onpaste = function() {
   // doSomething()
   return false; // to prevent user insert
}

The textInput event fires before the text is pasted, and is compatible with both ctrl+v and other ways of pasting text (like context menu's etc), it also has a property called .data which should/may hold the contents being pasted, but I've not seen this populated yet in browsers which do support the textInput event.

However, the input event fires /after/ the paste (when the dom has already changed), so I guess some coupling of the two of these could work in the interim.

Or.. you could just use the paste event ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!