正如您在代码片段中看到的那样,我能够获得粘贴到输入中的字符串的值。我想知道是否有一种简单的方法来获得被剪切的字符串的值。
$('#example').on("paste cut", function(e) {
var value = '';
if(e.type == 'paste') {
value = e.originalEvent.clipboardData.getData('text');
} else if(e.type == 'cut') {
// How should I get the removed part of the string
}
$('#result').html(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<input type="text" id="example" value="Some random value" style="width:80%"></div>
<div id="result"></div>
发布于 2018-08-01 23:00:48
这个使用window.getSelection().toString()
的解决方案怎么样?
$('#example').on("paste cut", function(e) {
var value = e.type=='paste'
? e.originalEvent.clipboardData.getData('text')
: window.getSelection().toString();
console.log(e.type+': '+value);
$('#result').html(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<input type="text" id="example" value="Some random value" style="width:80%"></div>
<div id="result"></div>
https://stackoverflow.com/questions/51636083
复制相似问题