22

我有一个简单的过去事件

document.getElementById('paste_area').addEventListener('paste', function() {
    document.getElementById('notice').innerHTML='Text was successfully pasted!';
    alert('Pasted');
}, true);

可以在这里找到一个工作示例http://jsfiddle.net/XEQzz/

警告和通知将在粘贴前出现。如何在粘贴事件实际完成后延迟警报操作?

4

5 回答 5

34

您可以将警报放在setTimeout.

setTimeout(function() {alert('Pasted');}, 0);

这将延迟代码直到值更新之后。

请记住,this回调setTimeout中的值与封闭环境中的值不同。

如果您需要对 external 的引用this,这将是元素,然后在变量中引用它......

var self = this;
setTimeout(function() {alert(self.value);}, 0);

或使用.bind() (在大多数支持的浏览器中支持addEventListener。旧版 Safari 不支持。) ...

setTimeout(function() {alert(this.value);}.bind(this), 0);
于 2012-12-15T18:48:18.643 回答
2

setTimeout 似乎是最好的选择,你可以使用这样的东西来概括

// 对象定义

    function PasteMonitor(element)
    {
        if(typeof element == "string")
        {
            this.target = document.getElementById(element);
        }
        else if(typeof element == "object" || typeof element == "function")
        {
            this.target = element;
        }

        if(this.target != null && this.target != undefined)
        {
            this.target.addEventListener('paste',this.inPaste.bind(this),false);
            this.target.addEventListener('change',this.changed.bind(this),false);
        }
        this.oldstate = "";
    }
    PasteMonitor.prototype = Object.create({},{
        pasted:{ value: false, enumerable: true, configurable: true, writable: true },
        changed:{ value: function(evt){
            //elements content is changed
            if(typeof this.onChange == "function")
            {
                this.onChange(evt);
            }
            if(this.pasted)
            {
                if(typeof this.afterPaste == "function")
                {
                    this.afterPaste(evt);
                    this.pasted = false;
                }
            }
        }, enumerable: true, configurable: true, writable: true },
        inPaste:{ value: function(evt){
            var cancelPaste = false;
            if(typeof this.beforePaste == "function")
            {
                // process pasted data
                cancelPaste = this.beforePaste(evt);
            }
            if(cancelPaste == true)
            {
                evt.preventDefault();
                return false;
            }
            this.pasted = true;
            setTimeout(function(){
                var evt = document.createEvent("HTMLEvents");
                evt.initEvent("change", false, true);
                this.target.dispatchEvent(evt);
            }.bind(this),0);
        }, enumerable: true, configurable: true, writable: true }
    })
    PasteMonitor.prototype.constructor = PasteMonitor;

//PasteMonitor 用法

    //var element = document.getElementById('paste_area');
    var msgArea = document.getElementById('message');
    var myMonitor = new PasteMonitor("paste_area");
    //or use and object as argument => var myMonitor = new PasteMonitor(element);

    myMonitor.onChange = function(evt){
        if(this.pasted)
        {
            //do something on paste change
            msgArea.innerHTML = "onChange by paste";
            this.oldstate = this.target.value;
        }
        //check to prevent processing change event when the element loses focus if the change is done by paste
        else if(this.target.value != this.oldstate)
        {
            msgArea.innerHTML = "onChange by typing";
        }
    };
    myMonitor.afterPaste = function(evt){
       // do something after paste
        msgArea.innerHTML = "afterPaste";
    };
    myMonitor.beforePaste = function(evt){
        // do something before the actual paste
        msgArea.innerHTML = "beforePaste";
        //return true to prevent paste
        return false;
    };
于 2012-12-15T23:38:33.470 回答
2

用于更新自身价值

 $(".number").on("paste", function (e) {
        var Value = e.originalEvent.clipboardData.getData('text');
        var Self=this
        setTimeout(function () {
            if (Value != Value.replace(/[^\d\.]/g, "")) {
                $(Self).val(Value.replace(/[^\d\.]/g, ""));
            }
        }, 0);
    });
于 2017-10-04T09:37:11.330 回答
1

我通常这样做是为了模仿 onafterpaste。

我有一个名为“onafterpaste”的特殊功能,我像这样使用它:

onpaste="onafterpaste(myFunction, this)"

如您所见,它还接受一个额外的参数,该参数将在调用 myFunction 时使用。

请参阅(并在其中尝试)以下实时代码段:

function onafterpaste(f,elm) {
  setTimeout(function() {f(elm)},0);
}

function  myFunction(elm) {
  alert('After the paste, the input has the following text:\r\n\r\n'+elm.value);
}
<p>Paste something down here:</p>
<input type="text" onpaste="onafterpaste(myFunction,this)" value="The original text">

于 2020-08-01T12:12:21.530 回答
-1

paste_area输入吗? input也是一个事件

document.getElementById('paste_area').addEventListener('input', 
    function(event) {
        alert('Pasted');
});
于 2022-01-28T19:18:19.450 回答