Text deselected on right click in IE8

倖福魔咒の 提交于 2019-12-07 12:55:24

问题


I want the user selection from a text area in my page. I have a context menu that user can use on right-clicking the selection. This is my code to retrieve selected content from a textarea in IE8,

var textComponent = document.getElementById('myTextArea');
var selectedText;
// IE version
if (document.selection != undefined)
{
textComponent.focus();
selectedText= document.selection.createRange();
alert(sel.text);
}

Now I notice that before allowing the blocked content(i.e the javascript) I can select a text in the text area and on right click it does not get deselected. But, when I allow the script, on right clicking the text that i selected gets deselected. Which is why i cannot retrieve the content.

I searched the web but didn't get any solution. Can anyone pls tell me what the problem is?

Thanks a lot.


回答1:


Calling the focus() method of the texbox or using the focus() method of any element on the page will clear the text selection.

Take a look here for a great answer:

Keep text selection when focus changes



来源:https://stackoverflow.com/questions/7329493/text-deselected-on-right-click-in-ie8

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