简体   繁体   English

更改IE8中的选择和范围功能

[英]Changes to selection and range functionality in IE8

I'm having to debug a WYSIWYG javascript based HTML editor that is failing in IE8. 我必须调试IE8中失败的基于WYSIWYG javascript的HTML编辑器。 It's only designed for use in IE so that should simplify the solution. 它仅设计用于IE,因此应简化解决方案。 Here's the existing code that is failing: 这是失败的现有代码:

function isAllowed() {
    var sel
    var obj

        sel = foo.document.selection

        if (sel.type != "Control")
        {
            obj = sel.createRange().parentElement()
        } else {
            obj = sel.createRange()(0)
        }

        if (obj.isContentEditable) {
            foo.focus()
            return true
        } else {
            return false
        }
}

Essentially what is happening is that if you select some text and click say the insert image button it first runs this isAllowed function to see if the text you've selected is editable (ie within the iframe that is ContentEditable). 本质上发生的是,如果您选择一些文本并单击说“ 插入图像”按钮,它将首先运行isAllowed函数,以查看您选择的文本是否可编辑(即在ContentEditable的iframe中)。
This seems to be breaking down in IE8 at at either document.selection or createRange() . 这似乎在IE8中的document.selectioncreateRange()处被破坏了。

The problem is that when you don't select any text with your mouse and click somewhere in the editable region, sel.createRange().parentElement() seems to return an element outside of the iframe and it's thus not ContentEditable and the function returns false . 问题是,当您没有用鼠标选择任何文本并单击可编辑区域中的某个位置时, sel.createRange().parentElement()似乎返回了iframe之外的元素,因此它不是ContentEditable,并且函数返回错误的

I'm wondering if anyone could shed any insight on to what has changed in IE8's implementation of selections and ranges that would cause this behaviour? 我想知道是否有人能对IE8的选择和范围实现中的哪些变化引起此现象有任何见解?

Ok, the answer was pretty simple! 好的,答案很简单! It must be a change in the way IE8 keeps the focus on the iframe, by adding foo.focus(); 必须通过添加foo.focus()改变IE8保持对iframe的关注。 to the code below, everything works as expected. 到下面的代码,一切正常。 Hope this helps someone, but it probably won't if their code was written properly in the first place :) 希望这对某人有帮助,但是如果他们的代码一开始就正确编写的话,可能就不会有用:)

function isAllowed() {
    var sel;
    var obj;

        foo.focus();
        sel = foo.document.selection;

        if (sel.type != "Control")
        {
            var rng = sel.createRange();
            obj = rng.parentElement();
        } else {
            obj = sel.createRange()(0);
        }

        if (obj.isContentEditable) {
            foo.focus();
            return true;
        } else {
            return false;
        }
}

Have you considered using a debugger or putting alerts into the javscript so you can figure out what is happening. 您是否考虑过使用调试器或将警报放入javscript中,以便了解发生了什么情况。 Figure out which element is being returned and maybe you will find your problem. 找出要返回的元素,也许您会发现问题。 It's possible it returning some parent element instead of the iframe (I'm just guessing here). 有可能返回一些父元素而不是iframe(我只是在这里猜测)。 Also, I am not sure why it would run if they have only clicked in the area though. 另外,我不确定如果他们只单击该区域,为什么它将继续运行。 Maybe I misunderstanding something. 也许我误会了一些东西。

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

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