开发者

What event is fired when text is cut or pasted (or ctrl+z'd) in a textarea?

开发者 https://www.devze.com 2023-01-31 02:08 出处:网络
I\'ve been using keyup to detect when content in a textarea changes, but somehow Facebook can detect a Ctrl+X开发者_运维知识库 event in a textbox immediately when the X is pressed down.What event woul

I've been using keyup to detect when content in a textarea changes, but somehow Facebook can detect a Ctrl+X开发者_运维知识库 event in a textbox immediately when the X is pressed down. What event would this be?


Most current browsers support cut, copy and paste events. Try the following to prove this to yourself:

<textarea oncut="alert('Cut!')" rows="3" cols="40"></textarea>


I would guess they make a bind to the keydown, and set some sort of state variable when the control key is pressed, then when they receive a keyup event, they check the state variable and act accordingly.


Hehehe I figured it out on my own!

In the keypress event for the textarea:

window.setTimeout((function(self) {
    return function() {
        console.log(self.value);
        }
    })(this), 0);

This will give the current value of the textarea as opposed to the value before the key was pressed. I've only verified this on Firefox 4 so far though.

Now my autogrowing textbox is as nice as Facebook's!

0

精彩评论

暂无评论...
验证码 换一张
取 消