IE onpaste JavaScript event

2020-03-29 04:15发布

No matter what I do, I can't get IE to select() the value in a after a "paste" event in canceled.

Look at this URL in IE, and paste anything into the textarea:

http://jsfiddle.net/cfApa/1/

The text should be selected after pasting. This works in Chrome, but can't figure out how to get select() to work when i'm canceling the paste even in IE:

2条回答
来,给爷笑一个
2楼-- · 2020-03-29 04:50

Take a look at this: http://webcloud.se/log/Selecting-text-with-JavaScript/. I think that's the issue, and you can make a workaround by using

var range = this.createTextRange();
range.moveStart("character",0);
range.moveEnd("character",$(this).html().length);
range.select();
查看更多
The star\"
3楼-- · 2020-03-29 04:58

Try this:

$('textarea').on('paste', function(e) {
    this.value = 'fooo';
    setTimeout(function(){ $(e.target).select(); }, 0);

    return false; 
});

Don't ask me why it works, I just got curious and found a solution.

It seems that selecting just doesn't work in the context of the paste event handler, maybe there's something that happens afterwards in the browser that deselects (though preventDefault still didn't help)

查看更多
登录 后发表回答