Javascript 在IE8中单击鼠标右键时取消选中的文本

Javascript 在IE8中单击鼠标右键时取消选中的文本,javascript,internet-explorer-8,right-click,Javascript,Internet Explorer 8,Right Click,我希望用户从我的页面中的文本区域进行选择。我有一个上下文菜单,用户可以使用右键单击选择。这是我从IE8中的文本区域检索所选内容的代码 var textComponent = document.getElementById('myTextArea'); var selectedText; // IE version if (document.selection != undefined) { textComponent.focus(); selectedText= document.selecti

我希望用户从我的页面中的文本区域进行选择。我有一个上下文菜单,用户可以使用右键单击选择。这是我从IE8中的文本区域检索所选内容的代码

var textComponent = document.getElementById('myTextArea');
var selectedText;
// IE version
if (document.selection != undefined)
{
textComponent.focus();
selectedText= document.selection.createRange();
alert(sel.text);
}
现在我注意到,在允许被阻止的内容(即Javascript)之前,我可以在文本区域中选择一个文本,右键单击它不会被取消选择。 但是,当我允许脚本时,右键单击我选择的文本将被取消选择。这就是我无法检索内容的原因

我在网上搜索了一下,但没有找到任何答案。有人能告诉我问题出在哪里吗?

调用texbox的focus()方法或使用页面上任何元素的focus()方法将清除文本选择

看看这里,可以找到一个很好的答案: