简体   繁体   English

getSelection()不适用于 <iframe> 在IE中

[英]getSelection() not working for <iframe> in IE

Leading on from my other question , which was answered by Elian Ebbing . 从我的另一个问题 出发,艾莲•埃宾Elian Ebbing回答了。 I now need to get this working for an iframe (dont ask). 我现在需要针对iframe使用此功能(不要问)。

It's basically a wisiwig editor that uses an iframe. 基本上,它是使用iframe的wisiwig编辑器。

I've coded up the test environment on jsfiddle . 我已经在jsfiddle上编写了测试环境。

Code is as follows: 代码如下:

CSS: CSS:

h1{font-size:150%; border-bottom:1px solid #ddd; margin:20px auto 10px;}

HTML: HTML:

<h1>Normal Text (works)</h1>
<p>Alex Thomas</p>
<button id="click">Click</button>

<h1>iFrame</h1>
<p>Type in some text:</p>
<iframe id="iframe"></iframe>
<br /><button id="iClick">Click</button>

jQuery jQuery的

(document).ready(function() {
    setTimeout(makeEdit,10);
});

$('#click').click(function(){
    var range = document.selection.createRange();
    range.pasteHTML("<span style='color: red'>" + range.htmlText + "</span>");
});

$('#iClick').click(function(){
    var range = document.selection.createRange();
    range.pasteHTML("<span style='color: red'>" + range.htmlText + "</span>");
});

function makeEdit(){
  document.getElementById("iframe").contentWindow. document.designMode="on";
};

I really hope someone can help me out... Thanks 我真的希望有人能帮助我...谢谢

You need to use the iframe's document object to create the TextRanges you use: 您需要使用iframe的document对象来创建要使用的TextRanges:

function makeIframeSelectionRed() {
    var range = document.getElementById("iframe").contentWindow.document.selection.createRange();
    range.pasteHTML("<span style='color: red'>" + range.htmlText + "</span>");
}

$('#click').click(makeIframeSelectionRed);

$('#iClick').click(makeIframeSelectionRed);

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

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