粘贴内容后,RadEditor会滚动到顶部

时间:2018-02-16 09:39:48

标签: javascript firefox telerik radeditor

我正在使用Telerik RadEditor。

问题:

将内容粘贴到Telerik Radeditor中后,编辑器将滚动到顶部。所以,我搜索了互联网和Telerik论坛,但没有运气。最后,我使用fix来处理以下代码。

RadEditor服务器端控制代码:

     <telerik:RadEditor 
                        ID="E" 
                        runat="server" 
                        Width="100%" 
                        AllowScripts="true" 
                        AllowPaging="true" 
                        EnableResize="false"
                        NewLineMode="P" 
                        OnClientLoad="OnClientLoad" 
                        OnClientModeChange="E_OnClientModeChanged" 
                        EditModes="Design,Html"
    StripFormattingOptions="Span,MsWordRemoveAll,CSS,Font,ConvertWordLists" 
                      OnClientPasteHtml="radEditorControl_OnClientPasteHtml" 
                      OnClientSelectionChange="OnClientSelectionChange">


</telerik:RadEditor>

以下是修复后的Javascript代码:

//Event on pasting the content into the RadEditor. Fixing the scrolling issue on pasting the content.

        var isPasted = false;

        function radEditorControl_OnClientPasteHtml(editor, args) {
            if (editor != undefined)
                isPasted = true;
        }

function OnClientSelectionChange(editor, args) {
            if (isPasted != undefined && isPasted == true && editor != undefined) {
                isPasted = false;
                editor.setFocus();
                var iframeElement = editor._contentAreaElement; // the DOM element for the iframe;

                if (iframeElement) {
                    //Get the iFrame cursor position.
                    var _focusNode = iframeElement.contentWindow.getSelection().focusNode;

                    if (_focusNode != undefined) {
                        //Focus node is not an element, get the previous element sibling
                        if (_focusNode.nodeType != 1) {
                            if (_focusNode.previousElementSibling != undefined)
                                _focusNode = _focusNode.previousElementSibling;
                            else
                                _focusNode = _focusNode.parentNode;
                        }

                        //Insert a div near focusNode(cursor) of the iFrame contentWindow.
                        var timeStamp = new Date().getUTCMilliseconds();
                        var _id = "scrollArea_" + timeStamp;

                        var focusElement = jQuery('<input type="text" id="' + _id + '" name="' + _id + '" onfocus="alert();" />').insertAfter(_focusNode)[0];

                        setTimeout(function () {
                            if (focusElement != undefined) {
                                focusElement.focus({ preventScroll: true });
                                focusElement.select();
                                var range = editor.getSelection().getRange(true); //returns an object that represents a restore point.
                                editor.getSelection().selectRange(range);
                                if (range != undefined && range.collapse !=undefined)
                                    range.collapse(true);

                                jQuery(focusElement).replaceWith("");
                            }
                        }, 50);
                    }
                }
            }
        }

上面的代码在IE和Chrome中有效,但在Firefox中没有,因为我没有为我绑定的输入元素触发focus()。

如果有解决RadEditor中粘贴滚动问题的其他方法,请告诉我。

1 个答案:

答案 0 :(得分:0)

我知道在旧版本的RadEditor中存在这样的问题。我的建议是尝试可以在https://demos.telerik.com/aspnet-ajax/editor/examples/overview/defaultcs.aspx测试的最新版本。如果问题仍然存在,请在Telerik AJAX论坛中或通过支持票务系统给我们留言,我们将对其进行更深入的研究。