How to alert after paste event in Javascript?

前端 未结 4 2124
无人及你
无人及你 2020-12-15 05:46

I have a simple past event as

document.getElementById(\'paste_area\').addEventListener(\'paste\', function() {
    document.getElementById(\'notice\').innerH         


        
4条回答
  •  太阳男子
    2020-12-15 06:30

    You could put your alert in a setTimeout.

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

    This will delay the code until after the value has updated.

    Just keep in mind that this in the setTimeout callback will have a different value than that in the enclosing environment.

    If you'll need a reference to the outer this, which will be the element, then reference it in a variable...

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

    Or use .bind() (Supported in most browsers that support addEventListener. Older Safari didn't support it.)...

    setTimeout(function() {alert(this.value);}.bind(this), 0);
    

提交回复
热议问题