iframeElm.contentWindow.document.selection not working for <iframe> in IE if set focus on other input after making selection in the iframe

那年仲夏 提交于 2019-12-24 15:49:31

问题


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:


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/



来源:https://stackoverflow.com/questions/16197000/iframeelm-contentwindow-document-selection-not-working-for-iframe-in-ie-if-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!