小编And*_*sch的帖子

在插入位置(IE)将文本插入可编辑的IFRAME

我正在努力解决一个实际上很直接的问题:在Internet Explorer中,我想在当前的插入位置插入纯文本.这对于简单的TEXTAREA元素非常有用,但它对于可编辑的IFRAME并不完全有效,这就是我所拥有的.

在我使用的脚本中,我正在从IFRAME的文档创建一个TextRange对象,我用它将文本粘贴到光标位置的HTML.

<iframe id="editable">
  <html>
    <body>
      Some really boring text.
    </body>
  </html>
</iframe>
<script type="text/javascript">

    window.onload = function() {
        var iframe = document.getElementById('editable');
        var doc = iframe.contentDocument || iframe.contentWindow.document;

        doc.body.innerHTML = iframe.textContent || iframe.innerHTML;

        // Make IFRAME editable
        if (doc.body.contentEditable) {
            doc.body.contentEditable = true;
        } 
    }

    function insert(text) {
        var iframe = document.getElementById('editable');
        var doc = iframe.contentDocument || iframe.contentWindow.document;
        iframe.focus();
      if(typeof doc.selection != 'undefined') {
            var range = doc.selection.createRange();
            range.pasteHTML(text);
      }
}
</script>
<input type="button" value="Insert" onClick="insert('foo');"/>
Run Code Online (Sandbox Code Playgroud)

当我在IFRAME中选择一些文本时,选择将替换为"foo" - …

javascript iframe internet-explorer textrange

4
推荐指数
1
解决办法
7806
查看次数

标签 统计

iframe ×1

internet-explorer ×1

javascript ×1

textrange ×1