【问题标题】:selected text in iframeiframe 中选定的文本
【发布时间】:2009-11-13 22:52:21
【问题描述】:

如何在 iframe 中获取选定的文本。

我的页面有一个可编辑的 iframe。 那么如何在该 iframe 中获取选定的文本。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

忽略我写的,那是垃圾。

这是真正的交易:

var iframe= document.getElementById('yourFrameId');
var idoc= iframe.contentDocument || iframe.contentWindow.document; // For IE.
alert(idoc.getSelection());

以上内容是从bobince'sthis SO Question回复公然盗用的

【讨论】:

    【解决方案2】:

    不确定 iframe 的可编辑 true 是什么意思。可能是文本字段或文本区域。但是如果你真的有一个 iframe 并且其中的一些文本被选中,那么这里有一个代码段应该可以在许多浏览器上工作:

    var iframe = document.getElementById("frame1");
    var txt = "";
    if (iframe.contentWindow.getSelection) {
        txt = iframe.contentWindow.getSelection();
    } else if (iframe.contentWindow.document.getSelection) {
        txt = iframe.contentWindow.document.getSelection();
    } else if (iframe.contentWindow.document.selection) {
        txt = iframe.contentWindow.document.selection.createRange().text;
    }
    

    【讨论】:

      【解决方案3】:

      我不确定所选文本的确切含义,但这是您使用 jquery 访问 iframe 内容的方式

      $('#iFrameID').contents().find('#whatImLookingFor').text(getSelectedText());
      
      function getSelectedText(){
          if(window.getSelection){
              return window.getSelection().toString();
          }
          else if(document.getSelection){
              return document.getSelection();
          }
          else if(document.selection){
              return document.selection.createRange().text;
          }
      } 
      

      【讨论】:

      • 意义中的选定文本(使用鼠标选定的文本)
      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多