[英]Handling paste by mouse. When pasting with ctrl + v the event.target.value is set to whatever string I pasted but when pasting but mouse it is not
When pasting into an input field of my widget with ctrl + v the event.target.value
where event
is an keyup
event is set to whatever string I pasted. 当使用ctrl + v粘贴到我的小部件的输入字段时, event.target.value
其中event
是一个keyup
事件)设置为我粘贴的任何字符串。 When doing paste by mouse using context menu event.target.value
whever event
is an paste
event whatever was in the input field before pasting, not what I want to paste. 使用上下文菜单通过鼠标进行粘贴时, event.target.value
whever event
是paste
事件,无论粘贴之前输入字段中的内容是什么,而不是我想要粘贴的事件。
Is it any common issue, how to handle the paste event after the contents are in the input field? 是否存在任何常见问题,请在输入字段中的内容之后处理粘贴事件?
The paste
event fires first, then event.target.value
changes. 首先触发paste
事件,然后更改event.target.value
。 That's why you get old value. 这就是为什么您会获得旧的价值。 Look here for the details https://www.w3.org/TR/clipboard-apis/ 在这里查看详细信息https://www.w3.org/TR/clipboard-apis/
You can handle it with addEventListener
for paste event and operating with clipboardData
. 您可以使用addEventListener
处理粘贴事件并使用clipboardData
。 Look through example below. 看下面的例子。
document.getElementById('testPaste2').addEventListener('paste', function(ev){ if(ev.clipboardData.types.indexOf('text/plain') > -1){ alert(ev.clipboardData.getData('text/plain')); } }); var inputs = document.getElementsByTagName('input'); for(i=0; i<inputs.length; i++){ inputs[i].addEventListener('keyup', function(ev){ alert(this.value) }); }
<p><label for="testPaste1">Without event listener <input type="text" id="testPaste1"></label></p> <p><label for="testPaste2">With event listener <input type="text" id="testPaste2"></label></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.