jQuery获得CUT的价值 [英] jQuery get the value of CUT
本文介绍了jQuery获得CUT的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可能会在摘要中看到
在输入中粘贴的字符串. 我想知道是否有一种简单的方法来获取CUTED字符串的值.
As you may see in the snippet I'm able to get the value of the string that is PASTED in the input. I was wondering if there's a simple way to get the value of the CUTED string.
$('#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>
推荐答案
使用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>
这篇关于jQuery获得CUT的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文