【问题标题】:how to get selected text from iframe with javascript?如何使用 javascript 从 iframe 中获取选定的文本?
【发布时间】:2010-12-01 02:26:19
【问题描述】:
<html>
 <body>
  <script type="text/javascript">

   function smth() {

    if (document.getSelection) {
    var str = document.getSelection();
    if (window.RegExp) {
      var regstr = unescape("%20%20%20%20%20");
      var regexp = new RegExp(regstr, "g");
      str = str.replace(regexp, "");
    }
    } else if (document.selection && document.selection.createRange) {
     var range = document.selection.createRange();
     var str = range.text;
    }   

    alert(str);
   }
  </script>   

    <iframe id="my"  width="300" height="225">
   .....some html ....
    </iframe>      

    <a href="#" onclick="smth();">AA</a>
 </body>    
</html>

使用smth 函数,我可以从某些 div 中获取选定的文本,但它不适用于 iframe。 任何想法如何从 iframe 中获取选定的文本?

【问题讨论】:

    标签: javascript iframe selected


    【解决方案1】:

    document.getSelection

    在外部文档上。要在 iframe 中选择文档,您需要获取内部文档:

    var iframe= document.getElementById('my');
    var idoc= iframe.contentDocument || iframe.contentWindow.document; // ie compatibility
    
    idoc.getSelection()
    

    但是请注意,WebKit 不支持document.getSelection() document.selection。尝试用 window.getSelection() 替换它,它在 Firefox 和 WebKit 中都可以使用,但返回一个选择对象(范围周围的集合/包装器),它需要字符串:

    var idoc= iframe.contentDocument || iframe.contentWindow.document;
    var iwin= iframe.contentWindow || iframe.contentDocument.defaultView;
    
    ''+iwin.getSelection()
    

    我不确定这是什么意思:

    if (window.RegExp) {
      var regstr = unescape("%20%20%20%20%20");
      var regexp = new RegExp(regstr, "g");
      str = str.replace(regexp, "");
    }
    

    RegExp 是可以追溯到最早版本的基本 JavaScript;它会一直在那里,你不必闻它。多个空格的 URL 编码是完全没有必要的。你甚至不需要 RegExp,字符串替换可以写成:

    str= str.split('     ').join('');
    

    【讨论】:

    • window.getSelection() 返回一个选择对象,而不是一个范围。选择上的 toString 将为您提供选择文本。
    • 是的,''+ 是 toString 的一个成语。
    • 是的,我知道。我正在纠正一个小错误,您将 window.getSelection() 返回的选择对象称为 Range,但同意您的代码可以工作。
    • 哦!是的,我明白你的意思。会解决的,ta!
    • 我正在为 iframe 获取未定义的 contentWindow。似乎为 iframe 获取窗口的正确方法是使用“window.frames”。 window.frames[0].getSelection() 给了我想要的东西
    【解决方案2】:

    您需要从 iframe 中的文档/窗口中获取选择。

    function getIframeSelectionText(iframe) {
      var win = iframe.contentWindow;
      var doc = win.document;
    
      if (win.getSelection) {
        return win.getSelection().toString();
      } else if (doc.selection && doc.selection.createRange) {
        return doc.selection.createRange().text;
      }
    }
    
    var iframe = document.getElementById("my");
    alert(getIframeSelectionText(iframe));
    

    【讨论】:

      【解决方案3】:

      您无法访问iframe 中的数据,这些数据来自不同于您的域。 这是由于Same origin policy

      【讨论】:

      • 好的,对不起,错误的例子,在 iframe 中我有文本,我使用 iframe 作为所见即所得的编辑器,所以它不在不同的域上。
      【解决方案4】:

      以下代码将返回选定的文本。

      function getSelectedText(frameId) { 
          // In ExtJS use: 
          // var frame = Ext.getDom(frameId); 
          var frame = document.getElementById(frameId); 
      
          var frameWindow = frame && frame.contentWindow; 
          var frameDocument = frameWindow && frameWindow.document; 
      
          if (frameDocument) { 
              if (frameDocument.getSelection) { 
                  // Most browsers 
                  return String(frameDocument.getSelection()); 
              } 
              else if (frameDocument.selection) { 
                  // Internet Explorer 8 and below 
                  return frameDocument.selection.createRange().text; 
              } 
              else if (frameWindow.getSelection) { 
                  // Safari 3 
                  return String(frameWindow.getSelection()); 
              } 
          } 
      
          /* Fall-through. This could happen if this function is called 
             on a frame that doesn't exist or that isn't ready yet. */ 
          return ''; 
      }
      

      希望这对某人有所帮助。

      【讨论】:

      • 在控制台的 Firefox 中:错误:访问属性“文档”的权限被拒绝”。它在行中:“var frameDocument = [...]”。
      【解决方案5】:

      此代码适用于所有现代浏览器:

      var iframe = document.getElementById('my');
      var idoc = iframe.contentDocument || iframe.contentWindow.document; // ie compatibility
      var text = idoc.getSelection().toString();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 1970-01-01
        相关资源
        最近更新 更多