我试过了(jsfiddle),但它没有用.
你怎么能看到警报是空的.就像.val()函数在复制字符串之前启动.
$(document).on('paste', '#pasteIt', function(){
alert($("#pasteIt").val());
var withoutSpaces = $("#pasteIt").val();
withoutSpaces = withoutSpaces.replace(/\s+/g, '');
$("#pasteIt").text(withoutSpaces);
});
Run Code Online (Sandbox Code Playgroud)
为什么?
Pra*_*lan 10
获取剪贴板数据
$(document).on('paste', '#pasteIt', function(e) {
e.preventDefault();
// prevent copying action
alert(e.originalEvent.clipboardData.getData('Text'));
var withoutSpaces = e.originalEvent.clipboardData.getData('Text');
withoutSpaces = withoutSpaces.replace(/\s+/g, '');
$(this).val(withoutSpaces);
// you need to use val() not text()
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="pasteIt" placeholder="Paste something here">Run Code Online (Sandbox Code Playgroud)
参考:https://forum.jquery.com/topic/paste-event-get-the-value-of-the-paste#14737000004101955
| 归档时间: |
|
| 查看次数: |
2239 次 |
| 最近记录: |