如何捕获粘贴事件的输入值? [英] How do I capture the input value on a paste event?

查看:81
本文介绍了如何捕获粘贴事件的输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,用户可以将文本(本例中为url)粘贴到输入字段中。我想捕获使用jQuery粘贴的文本的值。我使用下面的代码在FF中工作,但它在IE中不起作用(我不认为IE支持粘贴事件)。

On my site users can paste text (in this case a url) into an input field. I'd like to capture the value of the text that was pasted using jQuery. I've got this to work in FF using the code below, but it doesn't work in IE (I don't think IE supports the "paste" event).

任何人都知道如何在所有现代浏览器中实现这项工作?我已经在SO上发现了一些其他的答案,但大多数都是FF-only,而且似乎没有提供完整的解决方案。

Anyone know how to make this work across all modern browsers? I've found a few other answers to this on SO but most are FF-only and none seemed to offer a complete solution.

以下是我到目前为止的代码:

Here's the code I have so far:

$("input.url").live('paste', function(event) {
    var _this = this;
    // Short pause to wait for paste to complete
    setTimeout( function() {
        var text = $(_this).val();
        $(".display").html(text);
    }, 100);
});

JSFiddle: http://jsfiddle.net/TZWsB/1/

JSFiddle: http://jsfiddle.net/TZWsB/1/

推荐答案

jQuery有问题在IE中使用活动方法粘贴事件;解决方法:

jQuery has a problem with the live-method with the paste-event in the IE; workaround:

$(document).ready(function() {
    $(".url").bind('paste', function(event) {
        var _this = this;
        // Short pause to wait for paste to complete
        setTimeout( function() {
            var text = $(_this).val();
            $(".display").html(text);
        }, 100);
    });
});

小提琴: http://jsfiddle.net/Trg9F/

这篇关于如何捕获粘贴事件的输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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