开发者

Text deselected on right click in IE8

开发者 https://www.devze.com 2023-04-03 08:09 出处:网络
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,

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 please tell me what the problem is?


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

0

精彩评论

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