简体   繁体   English

IE8 iframe DesignMode失去选择

[英]IE8 iframe DesignMode loses selection

The example below is a simple example of an iframe which uses window.parent.[turn designmode on] (this works). 下面的例子是一个使用window.parent的iframe的简单例子。[turn designmode on](这是有效的)。 In FF all is well, but in IE8 (surprise surprise) any selection made is lost when you click out of the iframe. 在FF中一切都很好,但在IE8(惊喜)中,当你点击iframe时,任何选择都会丢失。 This totally negates the use of tools outside the iframe. 这完全否定了iframe之外的工具的使用。 I cannot find a solution 4 days later... 4天后我找不到解决办法......

Open in IE8 http://www.chromedigital.co.za/hydrapage/test.htm 在IE8中打开http://www.chromedigital.co.za/hydrapage/test.htm

On any element in the main document you want not to break the iframe selection, add unselectable="on" . 在主文档中的任何元素上,您不想破坏iframe选择,添加unselectable="on"

For example: 例如:

<div onclick="makeBold()" unselectable="on">Bold</div>

You could try saving the selection when the iframe loses focus. 当iframe失去焦点时,您可以尝试保存选择。 If you're sure the content of the iframe will not change before the user focuses on it again, you can store the currently selected Range or TextRange . 如果您确定iframe的内容在用户再次关注之前不会更改,则可以存储当前选定的RangeTextRange The following script (for the main page) includes your original script, is not extensively tested and would be improved with better feature detection but is something to work with: 以下脚本(对于主页面)包含您的原始脚本,未经过广泛测试,并且可以通过更好的功能检测进行改进,但可以使用:

h_FF=!document.all
h_rt_F=0

function HLC_DM()
{
 h_rt_F=document.getElementById("moo").contentWindow
 if(h_FF)
 {
  if(h_rt_F.document.designMode!="on")
  {
   try
   {
    h_rt_F.document.designMode="on"
    h_rt_F.document.execCommand("redo",false,null)
    createEventHandlers();
   }
   catch(e)
   {
    setTimeout("HLC_DM",200)
    return false
   }
  }
 }
 else
  h_rt_F.document.body.contentEditable=true
  createEventHandlers();
}


function getContentWindow() {
 return document.getElementById("moo").contentWindow;
}

function saveSelection() {
 var win = getContentWindow();
 var doc = win.document;
 var sel = win.getSelection ? win.getSelection() : doc.selection;
 var range;

 if (sel) {
  if (sel.createRange) {
   range = sel.createRange();
  } else if (sel.getRangeAt) {
   range = sel.getRangeAt(0);
  } else if (sel.anchorNode && sel.focusNode && doc.createRange) {
   // Older WebKit browsers
   range = doc.createRange();
   range.setStart(sel.anchorNode, sel.anchorOffset);
   range.setEnd(sel.focusNode, sel.focusOffset);

   // Handle the case when the selection was selected backwards (from the end to the start in the
   // document)
   if (range.collapsed !== sel.isCollapsed) {
    range.setStart(sel.focusNode, sel.focusOffset);
    range.setEnd(sel.anchorNode, sel.anchorOffset);
   }
  }
 }
 return range;
}

function restoreSelection(range) {
 var win = getContentWindow();
 var doc = win.document;
 var sel = win.getSelection ? win.getSelection() : doc.selection;

 if (sel && range) {
  if (range.select) {
   range.select();
  } else if (sel.removeAllRanges && sel.addRange) {
   sel.removeAllRanges();
   sel.addRange(range);
  }
 }
}

var selectedRange;

function blurHandler() {
 selectedRange = saveSelection();
}

function focusHandler() {
 if (selectedRange) {
  restoreSelection(selectedRange);
 }
}

var iframeHandlersCreated = false;
function createEventHandlers() {
 // Prevent setting up twice
 if (!iframeHandlersCreated) {
  var iframe = document.getElementById("moo");
  var doc;
  if (iframe.contentDocument && iframe.contentDocument.addEventListener) {
   doc = iframe.contentDocument;
   doc.addEventListener("blur", blurHandler, false);
   doc.addEventListener("focus", focusHandler, false);
  } else if (iframe.attachEvent) {
   iframe.attachEvent("onbeforedeactivate", blurHandler);
   iframe.attachEvent("onfocus", focusHandler);
  }
  iframeHandlersCreated = true;
 }
}

My Editbox can add images, tables etc where you last clicked in the iframe and works for ie6, ie7 and FF but for ie8 it adds then at the start. 我的编辑框可以添加您最后在iframe中单击的图像,表格等,适用于ie6,ie7和FF但是对于ie8,它会在开始时添加。 They can then be cut and pasted to where you want them but that is a nuisance. 然后可以将它们剪切并粘贴到您想要的位置,但这会令人讨厌。 MORE SERIOUS is that when I want to change the attributes of a table cell, for example, I have to have some text now in the cell which I must highlight or I can't determine what element I'm in! 更严重的是,当我想要更改表格单元格的属性时,例如,我必须在单元格中有一些文字,我必须突出显示或者我无法确定我在哪个元素!

Have Microsoft any bug fixes for the selection method or is Firefox or old versions of ie the only course? 微软是否有任何错误修复选择方法或Firefox或旧版本,即唯一的课程?

regards Mike W 关于迈克W

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

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