IE onpaste JavaScript event

蓝咒 提交于 2019-12-01 11:51:45

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)

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!