最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Intercept Paste data in JavaScript - Stack Overflow

programmeradmin2浏览0评论

I got the following code from Intercept paste event in Javascript.

I need to get it before it is pasted, otherwise i lose the "\n" characters i need to save.

It works great to intercept clipboard data for one element with an id. I need it to work on all input elements. When I try to use jQuery to get the input elements nothing.

Any help is appreciated.

var paster = function () {
    var myElement = document.getElementByTagName('pasteElement');
    myElement.onpaste = function(e) {
        var pastedText = undefined;
        if (window.clipboardData && window.clipboardData.getData) { // IE
            pastedText = window.clipboardData.getData('Text');
        } else if (e.clipboardData && e.clipboardData.getData) {
            pastedText = e.clipboardData.getData('text/plain');
        }
        processExcel(pastedText); // Process and handle text...
        return false; // Prevent the default handler from running.
    };
}

I got the following code from Intercept paste event in Javascript.

I need to get it before it is pasted, otherwise i lose the "\n" characters i need to save.

It works great to intercept clipboard data for one element with an id. I need it to work on all input elements. When I try to use jQuery to get the input elements nothing.

Any help is appreciated.

var paster = function () {
    var myElement = document.getElementByTagName('pasteElement');
    myElement.onpaste = function(e) {
        var pastedText = undefined;
        if (window.clipboardData && window.clipboardData.getData) { // IE
            pastedText = window.clipboardData.getData('Text');
        } else if (e.clipboardData && e.clipboardData.getData) {
            pastedText = e.clipboardData.getData('text/plain');
        }
        processExcel(pastedText); // Process and handle text...
        return false; // Prevent the default handler from running.
    };
}
Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Jul 2, 2014 at 20:49 wibberdingwibberding 8353 gold badges10 silver badges17 bronze badges 3
  • 2 there is no jquery in there. that's just plain javascript... – Marc B Commented Jul 2, 2014 at 20:50
  • Correct. I'm willing to do it either way. – wibberding Commented Jul 2, 2014 at 20:51
  • 1 so show your jquery code. we are not here to bang out a solution for you. – Marc B Commented Jul 2, 2014 at 20:52
Add a comment  | 

2 Answers 2

Reset to default 13

Just add a paste event listener to the document.

document.addEventListener("paste", function (e) {
    console.log(e.target.id);
    var pastedText = undefined;
    if (window.clipboardData && window.clipboardData.getData) { // IE
        pastedText = window.clipboardData.getData('Text');
    } else if (e.clipboardData && e.clipboardData.getData) {
        pastedText = e.clipboardData.getData('text/plain');
    }
    e.preventDefault();
    e.target.value = "You just pasted '" + pastedText + "'";
    return false;
});

fiddle

What nmaier said, but you also need to check for the original event.

document.addEventListener("paste", function (e) {
    console.log(e.target.id);
    var pastedText = undefined;
    if (window.clipboardData && window.clipboardData.getData) { // IE
        pastedText = window.clipboardData.getData('Text');
    } else {
        var clipboardData = (e.originalEvent || e).clipboardData;
        if (clipboardData && clipboardData.getData) {
            pastedText = clipboardData.getData('text/plain');
        }
        e.preventDefault();
        e.target.value = "You just pasted '" + pastedText + "'";
        return false;
    }
});

Also, you should probably add the event listener just to the element, instead of the whole document.

发布评论

评论列表(0)

  1. 暂无评论