簡體   English   中英

無法從window.getselection獲取任何值

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

在我們的項目中,我們一直在使用一種功能,當我們選擇一些文本並在其上單擊鼠標右鍵時,該文本將被復制,而再次單擊鼠標右鍵將導致復制的文本被粘貼到javascript中。 該代碼在IE11之前運行良好。 在IE11中,我們收到錯誤消息

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

我進行了很多研究,發現IE11不再支持document.selection,我們需要使用window.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(); 沒有任何效果。 我已經引用了這些鏈接

無法獲取未定義或空引用的屬性“ createRange”

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

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發展

這些dint幫助了他們要求使用window.getSelection()的任何地方。

這是我的代碼: 編輯:

請注意,以下代碼在IE7 / 8和chrome中均能正常運行,因為window.getSelection().toString()為空,因此在IE11上無法正常工作。 另外,請注意在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();
            }

        }

非常感謝您的任何幫助。

萬一它對任何人都有幫助。 我為此苦苦掙扎,最終發現這是因為我試圖在無效的上下文中使用window.getSelection()。 它不會在input:text或textarea中返回選定的文本。 相反,我不得不使用selectionStart和selectionEnd,然后從輸入元素中提取值並從中解析所選文本。 感謝http://help.dottoro.com/ljcvonpc.php提供的此信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM