将粘贴事件挂钩到隐藏文本区域

发布于 2024-08-20 11:10:16 字数 304 浏览 2 评论 0原文

我想挂钩 的粘贴事件,并强制将此文本粘贴到隐藏的文本区域中(然后我想解析文本区域的文本并执行“将数据从 excel 粘贴到gridview 的操作)。比如:

$('#input1').bind('paste', function(e) {
    // code do paste text to textarea instead of originally targeted input
});

我应该编写什么跨浏览器代码而不是注释?

谢谢。

I want to hook paste event for <input type="text"> and force this text to be pasted into hidden textarea (then I want to parse textarea's text and perform 'paste data from excel to gridview' action). Something like:

$('#input1').bind('paste', function(e) {
    // code do paste text to textarea instead of originally targeted input
});

What cross-browser code should I write instead of comments?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡墨 2024-08-27 11:10:16

有一个黑客解决方案,当按下 Ctrl 和 V 键或 Shift 和 Insert 键时,会在文本区域上触发 focus 事件。 [是的,它不适用于上下文菜单 ->过去]

$(document).ready(function(){
    var activeOnPaste = null;
    $('#input1').keydown(function(e){
        var code = e.which || e.keyCode;
        if((e.ctrlKey && code == 86) || (e.shiftKey && code == 45)){
            activeOnPaste = $(this);
            $('#textarea').val('').focus();
        }
    });
    $('#textarea').keyup(function(){
        if(activeOnPaste != null){
            $(activeOnPaste).focus();
            activeOnPaste = null;
        }
    });
});

当按下 Ctrl 和 V 键时,代码让指针聚焦在文本区域上。此时没有粘贴任何文本,而是在触发此 keydown 函数后粘贴文本,以便粘贴的文本显示在文本区域中。之后,在该文本区域上按键时,#input1 将获得焦点。

在输入此内容时,我发现可能有一个使用范围的键盘粘贴和鼠标粘贴的解决方案。我也会尝试一些东西......

There is this hacky solution that fires a focus event on a textarea when Ctrl and V keys or Shift and Insert keys are down. [Yes, it doesn't work for contextmenu -> past]

$(document).ready(function(){
    var activeOnPaste = null;
    $('#input1').keydown(function(e){
        var code = e.which || e.keyCode;
        if((e.ctrlKey && code == 86) || (e.shiftKey && code == 45)){
            activeOnPaste = $(this);
            $('#textarea').val('').focus();
        }
    });
    $('#textarea').keyup(function(){
        if(activeOnPaste != null){
            $(activeOnPaste).focus();
            activeOnPaste = null;
        }
    });
});

The code lets the pointer focus on a textarea when Ctrl and V keys are down. At that moment no text is pasted, it's pasted after this keydown function is fired so the pasted text is shown in the textarea. After that, on keyup on that textarea, #input1 will be focused.

While typing this, I see that there may be a solution for both keyboard pasting and mouse pasting, using ranges. I'll try something with that too...

美羊羊 2024-08-27 11:10:16

您应该将一个函数绑定到输入字段的 onChange() 事件,并在每次调用该函数时复制其内容,然后处理数据。如果您对“粘贴”内容特别感兴趣(我不知道您想在那里做什么,但通常情况下,必须另外处理粘贴内容是一个不好的概念的标志),您可以尝试实施检查输入速度的计数器(例如每秒超过 xx 个字符 -> PASTE-Eventcall)

You should bind a function to your input-fields onChange() event and copy its content everytime this function is called and process the data afterwards. If you are specifically interested in "pasted" content (I do not know what you are trying to do there, but generally it is a sign of bad concept to be in a situation where pasted content has to be treated additionally) you can try implementing a counter that checks the input speed (eg more than xx characters per second -> PASTE-Eventcall)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文