iframeElm.contentWindow.document.selection not wor

2019-08-11 10:16发布

This is similar but different question about how to set html in the selection of the iframe's editable content.

One sample found is: http://jsfiddle.net/HMRMv/1/, but it can also be used to show the problem.

On IE, in the sample if you select the text in iframe and then do the click, it will work. But after select the text in the iframe's editable content and than clicking on other pane, you will see the highlight disappear and the 'click' on the button will do nothing. Look like the:

var range = document.getElementById("iframe").contentWindow.document.selection.createRange(); 

Cannot get the previous selected range.

I tried cache the range in blur and reuse it at the 'click', but it does not work. Is it to do with iframe, any suggestion on this case?

var selectedType, selectedRange;

function onBlur(e) {
    var selection = iframeElm.contentWindow.document.selection;
    var range = selection.createRange();

    selectedType= selection.type;
    selectedRange = range;
}

1条回答
Evening l夕情丶
2楼-- · 2019-08-11 10:51

The blur event is too late to cache the selection range because the selection is already destroyed by then. Use the IE-only beforedeactivate event on the iframe element instead.

Demo: http://jsfiddle.net/HMRMv/34/

查看更多
登录 后发表回答