如何使用 javascript 从 iframe 中获取选定的文本?

发布于 2024-08-05 11:35:24 字数 838 浏览 10 评论 0原文

<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 获取选定的文本吗?

<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>

with smth function i can get selected text from some div, but it doesnt work with iframe.
any ideas how to get selected text from iframe ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月依秋水 2024-08-12 11:35:24

文档.getSelection

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

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

idoc.getSelection()

但请注意,WebKit 不支持 document.getSelection() document。选择。尝试用 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('');

document.getSelection

Is on the outer document. To get the selection of the document in the iframe you need to grab the inner document:

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

idoc.getSelection()

Note however that WebKit does not support document.getSelection() or document.selection. Try replacing it with window.getSelection() which works in both Firefox and WebKit, but returns a selection object (a collection/wrapper around Ranges), which needs stringing:

var idoc= iframe.contentDocument || iframe.contentWindow.document;
var iwin= iframe.contentWindow || iframe.contentDocument.defaultView;

''+iwin.getSelection()

I'm not sure what the point of this is:

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

RegExp is basic JavaScript dating back to the very earliest version; it will always be there, you don't have to sniff for it. The URL-encoding of multiple spaces is quite unnecessary. You don't even need RegExp as such, a string replace could be written as:

str= str.split('     ').join('');
请你别敷衍 2024-08-12 11:35:24

您需要从 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));

You need to get the selection from the document/window in the 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));
最单纯的乌龟 2024-08-12 11:35:24

您无法访问来自与您所在域不同的域的 iframe 内的数据。
这是由于同源政策

You can't access data inside an iframe that is from a domain different than yours.
This is due to the Same origin policy.

单挑你×的.吻 2024-08-12 11:35:24

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

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 ''; 
}

希望这对某人有帮助。

Following code will return the selected text.

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 ''; 
}

Hope this will help to someone.

滥情稳全场 2024-08-12 11:35:24

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

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

This code works in all modern browsers:

var iframe = document.getElementById('my');
var idoc = iframe.contentDocument || iframe.contentWindow.document; // ie compatibility
var text = idoc.getSelection().toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文