获取用户从网页上复制的文本 [英] Obtain the text that a user is copying from a web page

查看:107
本文介绍了获取用户从网页上复制的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaScript,有可能检测用户何时从网页复制文本,然后将复制的文本存储在数组中(而不会覆盖CTRL + V组合键,并且不会阻止文本被复制) ?

换句话说,是否有可能在不覆盖该组合键的默认行为的情况下检测组合键?

解决方案在jQuery中使用 keyup 侦听器并获取事件参数 ctrlKey keyCode ,然后通过 window.getSelection()获取选定的文本:

  $(window).keyup(function(e){
if(e.ctrlKey&& e.keyCode === 67){
alert(Text copied was:+ window.getSelection());
}
});

http://jsfiddle.net/G5kAG/1/






默认行为除非你在监听函数中 return false (或者在jQuery中, e.preventDefault() >,它实际上(和代码中)和返回false 相同)。


Using JavaScript, is it possible to detect when a user is copying text from a web page, and then store the copied text in an array (without overriding the CTRL+V key combination, and without preventing the text from being copied)?

In other words, is it possible to detect a key combination without overriding the default behavior of that key combination?

解决方案

Using the keyup listener in jQuery and getting the event parameters ctrlKey and keyCode, then retrieving the selected text through window.getSelection():

$(window).keyup(function (e) {
    if (e.ctrlKey && e.keyCode === 67) {
        alert("Text copied was: " + window.getSelection());
    }
});

http://jsfiddle.net/G5kAG/1/


The default behaviour of the key strokes are not interfered with unless you return false in the listening function (or, in jQuery, e.preventDefault(), which in effect (and in code) does the same thing as return false).

这篇关于获取用户从网页上复制的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆