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

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

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Aug 6, 2021 at 4:36 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 19, 2010 at 0:59 NickNick 5,44011 gold badges43 silver badges71 bronze badges 2
  • Isn't that what onchange is for? – Gabe Commented Dec 19, 2010 at 1:12
  • 2 I could be wrong, but I thought onchange was only when the element lost focus.. – Nick Commented Dec 19, 2010 at 2:01
Add a ment  | 

3 Answers 3

Reset to default 4

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)

  1. 暂无评论