简体   繁体   English

无法从window.getselection获取任何值

[英]Unable to get any value from window.getselection

In our project we have been using a functionality that when we select some text and right click on it the text is copied and another right click will cause the copied text to be pasted in javascript. 在我们的项目中,我们一直在使用一种功能,当我们选择一些文本并在其上单击鼠标右键时,该文本将被复制,而再次单击鼠标右键将导致复制的文本被粘贴到javascript中。 The code was working fine until IE11. 该代码在IE11之前运行良好。 In IE11 we get error at 在IE11中,我们收到错误消息

textEl.caretPos = document.selection.createRange().duplicate(); 

I have researched a lot and found that document.selection is no more supported on IE11 and we need to use window.getSelection() but that didnt work either. 我进行了很多研究,发现IE11不再支持document.selection,我们需要使用window.getSelection(),但这也不起作用。 I have tried all combinations window.getSelection(); window.document.getSelection(); document.getSelection(); window.external.menuArguments.document.getSelection(); 我已经尝试了所有组合window.getSelection(); window.document.getSelection(); document.getSelection(); window.external.menuArguments.document.getSelection(); window.getSelection(); window.document.getSelection(); document.getSelection(); window.external.menuArguments.document.getSelection(); nothing works. 没有任何效果。 I have already reffered these links 我已经引用了这些链接

Unable to get property 'createRange' of undefined or null reference 无法获取未定义或空引用的属性“ createRange”

https://tracker.phpbb.com/browse/PHPBB3-12094 https://tracker.phpbb.com/browse/PHPBB3-12094

https://social.msdn.microsoft.com/Forums/ie/en-US/138e9cbc-aee7-46fc-bb7e-c5112e88497a/unable-to-get-property-createrange-of-undefined-or-null-reference?forum=ieextensiondevelopment https://social.msdn.microsoft.com/Forums/ie/zh-CN/138e9cbc-aee7-46fc-bb7e-c5112e88497a/unable-to-get-property-createrange-of-undefined-or-null-reference吗?论坛= ieextension发展

These dint help either as everywhere they have asked to use window.getSelection(). 这些dint帮助了他们要求使用window.getSelection()的任何地方。

Here is my code: EDIT: 这是我的代码: 编辑:

Please note the below code works fine in IE7/8 and chrome it doesnt work on IE11 as window.getSelection().toString() is empty. 请注意,以下代码在IE7 / 8和chrome中均能正常运行,因为window.getSelection().toString()为空,因此在IE11上无法正常工作。 Also note this in inside an Iframe if that creates any difference. 另外,请注意在iframe中是否存在任何差异。

/**
         * Copies or Pastes text into a text box.  If the
         * text is selected, then right clicking on it does a copy.
         * If no text is selected, then right clicking invokes a paste
         * of any clipboard text into the textbox.
         *
         * NOTE: Pasting will replace any value already in the textbox
         */
        function copyPasteHelper()
        {
            // if something is currently selected, copy it
            var selectedText = "";
            if(document.selection != null){// for IE 8 and below only
                storeCaret (event.srcElement);
                selectedText = document.selection.createRange().text;
            }else if(typeof window.getSelection() != "undefined") // for IE 9+ and Chrome
            {
                //storeCaret (event.srcElement);
                selectedText = window.getSelection().toString();
                alert(selectedText);//this is empty
            }


            if (selectedText != "")
            {
                if(window.clipboardData)
                {
                    window.clipboardData.setData("Text", selectedText);

                    var lefter2 = event.offsetY+0;
                    var topper2 = event.offsetX+15;

                    // oCopiedPopup.show(topper2, lefter2, 80, 23, window.event.srcElement);
                }
                else
                {
                    jQuery("#clipboard", window.parent.document).val(selectedText);
                }
            }
            else // if nothing is selected, paste whatever text is in the clipboard
            {
                pasteHelper();
            }

        }

Any help will be appreciated thanks a lot in advance. 非常感谢您的任何帮助。

In case it helps anybody. 万一它对任何人都有帮助。 I was struggling with this and finally figured out it was because I was trying to use window.getSelection() in an invalid context. 我为此苦苦挣扎,最终发现这是因为我试图在无效的上下文中使用window.getSelection()。 It does not return selected text in an input:text or textarea. 它不会在input:text或textarea中返回选定的文本。 Instead I had to use selectionStart and selectionEnd, and pull the value from the input element and parse the selected text from there. 相反,我不得不使用selectionStart和selectionEnd,然后从输入元素中提取值并从中解析所选文本。 Thanks to http://help.dottoro.com/ljcvonpc.php for this information. 感谢http://help.dottoro.com/ljcvonpc.php提供的此信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM